-
Notifications
You must be signed in to change notification settings - Fork 0
/
MimeFile.as
117 lines (94 loc) · 2.67 KB
/
MimeFile.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* MimeFile Class
*
* (C) David Verhasselt 2007
* Licensed under the MIT license
*/
package crowdway.http
{
import crowdway.http.MimePart;
import flash.utils.ByteArray;
import flash.events.*;
import mx.utils.StringUtil;
import flash.net.*;
/**
* Implements the "file" input-field
*
* @see MimePart
* @see MimeString
* @see HTTPForm
*/
public class MimeFile implements MimePart
{
private static const crlf:String = "\r\n";
// Content parameters
private var name:String;
private var fileName:String;
private var contentType:String;
private var charset:String;
private var content:ByteArray;
/**
* Factory Method: Creates a text/plain MimeFile with the supplied name, filename, content and optional charset.
*
* @param name The name of the new MimeFile
* @param fileName The filename of the new MimeFile
* @param content The content of the new MimeFile
* @param charset The optional charset of the content. Defaults to "us-ascii"
*
* @return A new MimeFile based on the supplied parameters
*/
static public function fromText(name:String,fileName:String,content:String,charset:String = "us-ascii"):MimeFile
{
var result:MimeFile = new MimeFile();
result.name = name;
result.fileName = fileName;
result.contentType = "text/plain";
result.content = new ByteArray();
result.content.writeMultiByte(content,charset);
result.charset = charset;
return result;
}
/**
* Factory Method: Creates a binary MimeFile with the supplied name, filename, content and optional content-type.
*
* @param name The name of the new MimeFile
* @param fileName The filename of the new MimeFile
* @param content The content of the new MimeFile
* @param contentType The optional content-type of the content. Defaults to "application/octet-stream"
*
* @return A new MimeFile based on the supplied parameters
*/
static public function fromByteArray(name:String,fileName:String,content:ByteArray,contentType:String = "application/octet-stream"):MimeFile
{
var result:MimeFile = new MimeFile();
result.name = name;
result.fileName = fileName;
result.contentType = contentType;
result.content = content;
return result;
}
public function getName():String
{
return this.name;
}
public function getFilename():String
{
return this.fileName;
}
public function getHead():String
{
var head:String = "Content-Type: ";
head += this.contentType;
if (this.charset != null)
{
head += "; charset=" + this.charset;
}
return head;
}
public function getBody():ByteArray
{
this.content.position = 0;
return this.content;
}
}
}