-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.js
134 lines (106 loc) · 3.88 KB
/
upload.js
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
function size(bytes){ // simple function to show a friendly size
var i = 0;
while(1023 < bytes){
bytes /= 1024;
++i;
};
return i ? bytes.toFixed(2) + ["", " Kb", " Mb", " Gb", " Tb"][i] : bytes + " bytes";
};
function RequestBuilder() {
this.header = function( name, value, params ) {
var crlf = '\r\n';
this.body += name + ": " + value;
if( params ) {
for( var count=params.length, i=0; i<count; i++ ) {
pair = params[i];
this.body += '; ' + pair[0] + '="' + pair[1] + '"';
}
}
this.body += crlf;
return this;
}
this.segment = function(headers, body) {
var dashdash = '--';
var crlf = '\r\n';
this.body += dashdash + this.boundary + crlf;
this.body += headers;
this.body += crlf;
this.body += body;
return this;
}
this.param = function(name, value) {
var dashdash = '--';
var crlf = '\r\n';
headers = 'Content-Disposition: form-data; name="' + name + '"' + crlf;
body = value + crlf;
this.segment(headers, body, this.boundary);
return this;
}
this.footer = function(){
var dashdash = '--';
var crlf = '\r\n';
this.body += dashdash + this.boundary + dashdash + crlf;
}
this.file = function(fieldname, filename, type, file_content) {
var dashdash = '--';
var crlf = '\r\n';
this.body += dashdash + this.boundary + crlf;
this.header( "Content-Disposition", "form-data", [ ['name', fieldname], ['filename', filename] ]);
this.header( "Content-Type", type);
this.body += crlf;
this.body += file_content;
this.body += crlf;
}
this.body = '';
this.output = function() {
return this.body;
}
this.dd = '--';
this.nl = '\r\n';
var id = (new Date).getTime();
}
function sendFile(handler){
var parameters = handler.parameters;
var xhr = new XMLHttpRequest;
//check size
if(handler.maxSize && handler.maxSize < handler.file.fileSize){
var msg = "The file " + handler.file.fileName + " is too big [" + size(handler.file.fileSize) + "]";
handler.onerror(msg);
return;
};
//setup callbacks
var eventnames = "onabort.onerror.onloadstart.onprogress".split(".");
for( i in eventnames ) {
var ev = eventnames[i];
xhr.upload[ev] = function(rpe){
handler[ ev ].call(handler, rpe, xhr);
};
}
//loop until finished
xhr.upload.onload = function(rpe){
var checkstatus = function(){
if(xhr.readyState === 4){
handler.onload(rpe, xhr);
} else
setTimeout(arguments.callee, 15);
};
setTimeout(checkstatus,15);
};
var file = handler.file;
var id = (new Date).getTime();
var builder = new RequestBuilder;
builder.boundary = '------multipartformboundary' + id;
builder.param( 'key', id + '-' + escape(file.fileName) );
builder.param( 'acl', parameters.acl );
builder.param( 'content-type', file.type );
builder.param( 'AWSAccessKeyId', parameters.aws_access_key );
builder.param( 'policy', parameters.policy );
builder.param( 'signature', parameters.signature );
builder.file( 'file', file.fileName, 'application/ocet-stream', file.getAsBinary() );
builder.footer();
var url = parameters.url;
xhr.open("POST", url, true);
xhr.setRequestHeader('content-type', 'multipart/form-data; boundary=' + builder.boundary);
xhr.sendAsBinary( builder.output() );
return handler;
};