forked from mailru/FileAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileAPI.Form.js
More file actions
160 lines (141 loc) · 3.76 KB
/
FileAPI.Form.js
File metadata and controls
160 lines (141 loc) · 3.76 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
(function (api, window, document){
var
encode = window.encodeURIComponent,
FormData = window.FormData,
Form = function (){
this.items = [];
}
;
Form.prototype = {
append: function (name, blob, file, type){
this.items.push({
name: name
, blob: blob && blob.blob || (blob == void 0 ? '' : blob)
, file: blob && (file || blob.name)
, type: blob && (type || blob.type)
});
},
each: function (fn){
var i = 0, n = this.items.length;
for( ; i < n; i++ ){
fn.call(this, this.items[i]);
}
},
toData: function (fn, options){
// allow chunked transfer if we have only one file to send
// flag is used below and in XHR._send
options._chunked = api.support.chunked && options.chunkSize > 0 && api.filter(this.items, function (item){ return item.file; }).length == 1;
if( !api.support.html5 ){
api.log('FileAPI.Form.toHtmlData');
this.toHtmlData(fn);
}
else if( this.multipart ){
api.log('FileAPI.Form.toMultipartData');
this.toMultipartData(fn);
}
else if( options._chunked ){
api.log('FileAPI.Form.toPlainData');
this.toPlainData(fn);
}
else {
api.log('FileAPI.Form.toFormData');
this.toFormData(fn);
}
},
_to: function (data, complete, next, arg){
var queue = api.queue(function (){
complete(data);
});
this.each(function (file){
next(file, data, queue, arg);
});
queue.check();
},
toHtmlData: function (fn){
this._to(document.createDocumentFragment(), fn, function (file, data/**DocumentFragment*/){
var blob = file.blob, hidden;
if( file.file ){
api.reset(blob);
// set new name
blob.name = file.name;
data.appendChild(blob);
}
else {
hidden = document.createElement('input');
hidden.name = file.name;
hidden.type = 'hidden';
hidden.value = blob;
data.appendChild(hidden);
}
});
},
toPlainData: function (fn){
this._to({}, fn, function (file, data, queue){
if( file.file ){
data.type = file.file;
}
if( file.blob.toBlob ){
// canvas
queue.inc();
file.blob.toBlob(function (blob){
data.name = file.name;
data.file = blob;
data.size = blob.length;
data.type = file.type;
queue.next();
}, 'image/png');
}
else if( file.file ){
//file
data.name = file.blob.name;
data.file = file.blob;
data.size = file.blob.size;
data.type = file.type;
}
else {
// additional data
if (!data.params) {
data.params = [];
}
data.params.push(encodeURIComponent(file.name) + "=" + encodeURIComponent(file.blob));
}
data.start = -1;
data.end = data.file.FileAPIReadPosition || -1;
data.retry = 0;
});
},
toFormData: function (fn){
this._to(new FormData, fn, function (file, data, queue){
if( file.file ){
data.append('_'+file.name, file.file);
}
if( file.blob && file.blob.toBlob ){
queue.inc();
file.blob.toBlob(function (blob){
data.append(file.name, blob, file.file);
queue.next();
}, 'image/png');
}
else if( file.file ){
data.append(file.name, file.blob, file.file);
}
else {
data.append(file.name, file.blob);
}
});
},
toMultipartData: function (fn){
this._to([], fn, function (file, data, queue, boundary){
data.push(
'--_' + boundary + ('\r\nContent-Disposition: form-data; name="'+ file.name +'"'+ (file.file ? '; filename="'+ encode(file.file) +'"' : '')
+ (file.file ? '\r\nContent-Type: '+ (file.type || 'application/octet-stream') : '')
+ '\r\n'
+ '\r\n'+ (file.file ? file.blob : encode(file.blob))
+ '\r\n')
);
}, api.expando);
}
};
// @export
api.Form = Form;
})(FileAPI, window, document);