Skip to content

Commit beaa4ed

Browse files
committed
+ _converFile method
1 parent 0cc04d8 commit beaa4ed

1 file changed

Lines changed: 56 additions & 20 deletions

File tree

lib/FileAPI.Form.js

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
"use strict";
55

66
var
7-
encode = window.encodeURIComponent
8-
, document = window.document
7+
document = window.document
98
, FormData = window.FormData
109
, Form = function (){ this.items = []; }
10+
, encodeURIComponent = window.encodeURIComponent
1111
;
1212

1313

@@ -90,31 +90,33 @@
9090
if( file.file ){
9191
data.type = file.file;
9292
}
93+
9394
if( file.blob.toBlob ){
9495
// canvas
9596
queue.inc();
96-
file.blob.toBlob(function (blob){
97+
_converFile(file, function (file, blob){
9798
data.name = file.name;
9899
data.file = blob;
99100
data.size = blob.length;
100101
data.type = file.type;
101102
queue.next();
102-
}, 'image/png');
103+
});
103104
}
104105
else if( file.file ){
105-
//file
106+
// file
106107
data.name = file.blob.name;
107108
data.file = file.blob;
108109
data.size = file.blob.size;
109110
data.type = file.type;
110111
}
111112
else {
112113
// additional data
113-
if (!data.params) {
114+
if( !data.params ){
114115
data.params = [];
115116
}
116-
data.params.push(encodeURIComponent(file.name) + "=" + encodeURIComponent(file.blob));
117+
data.params.push(encodeURIComponent(file.name) +"="+ encodeURIComponent(file.blob));
117118
}
119+
118120
data.start = -1;
119121
data.end = -1;
120122
data.retry = 0;
@@ -123,41 +125,75 @@
123125

124126
toFormData: function (fn){
125127
this._to(new FormData, fn, function (file, data, queue){
126-
if( file.file ){
127-
data.append('_'+file.name, file.file);
128-
}
129-
130128
if( file.blob && file.blob.toBlob ){
131129
queue.inc();
132-
file.blob.toBlob(function (blob){
130+
_converFile(file, function (file, blob){
133131
data.append(file.name, blob, file.file);
134132
queue.next();
135-
}, 'image/png');
133+
});
136134
}
137135
else if( file.file ){
138136
data.append(file.name, file.blob, file.file);
139137
}
140138
else {
141139
data.append(file.name, file.blob);
142140
}
141+
142+
if( file.file ){
143+
data.append('_'+file.name, file.file);
144+
}
143145
});
144146
},
145147

146148

147149
toMultipartData: function (fn){
148150
this._to([], fn, function (file, data, queue, boundary){
149-
data.push(
150-
'--_' + boundary + ('\r\nContent-Disposition: form-data; name="'+ file.name +'"'+ (file.file ? '; filename="'+ encode(file.file) +'"' : '')
151-
+ (file.file ? '\r\nContent-Type: '+ (file.type || 'application/octet-stream') : '')
152-
+ '\r\n'
153-
+ '\r\n'+ (file.file ? file.blob : encode(file.blob))
154-
+ '\r\n')
155-
);
151+
_converFile(file, function (file, blob){
152+
data.push(
153+
'--_' + boundary + ('\r\nContent-Disposition: form-data; name="'+ file.name +'"'+ (file.file ? '; filename="'+ encodeURIComponent(file.file) +'"' : '')
154+
+ (file.file ? '\r\nContent-Type: '+ (file.type || 'application/octet-stream') : '')
155+
+ '\r\n'
156+
+ '\r\n'+ (file.file ? blob : encodeURIComponent(blob))
157+
+ '\r\n')
158+
);
159+
});
156160
}, api.expando);
157161
}
158162
};
159163

160164

165+
function _converFile(file, fn){
166+
var blob = file.blob, filename = file.file;
167+
168+
if( filename ){
169+
var
170+
mime = { 'image/jpeg': '.jpe?g', 'image/png': '.png' }
171+
, type = mime[file.type] ? file.type : 'image/png'
172+
, ext = mime[type] || '.png'
173+
, quality = blob.quality || 1
174+
;
175+
176+
if( !filename.match(new RegExp(ext+'$', 'i')) ){
177+
filename += ext.replace('?', '');
178+
}
179+
180+
file.file = filename;
181+
file.type = type;
182+
183+
if( blob.toBlob ){
184+
blob.toBlob(function (blob){
185+
fn(file, blob);
186+
}, type, quality);
187+
}
188+
else {
189+
blob = api.toBinaryString(blob.toDataURL(type, quality));
190+
}
191+
}
192+
193+
return blob;
194+
}
195+
196+
161197
// @export
162198
api.Form = Form;
163199
})(FileAPI, window);

0 commit comments

Comments
 (0)