Skip to content

Commit 2695c28

Browse files
committed
Merge pull request #64 from wingedfox/master
Bugfix for #63
2 parents 0bab550 + e88e488 commit 2695c28

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

lib/FileAPI.Form.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
},
2828

2929
toData: function (fn, options){
30+
// allow chunked transfer if we have only one file to send
31+
// flag is used below and in XHR._send
32+
options._chunked = api.support.chunked && options.chunkSize > 0 && api.filter(this.items, function (item){ return item.file; }).length == 1;
33+
3034
if( !api.support.html5 ){
3135
api.log('FileAPI.Form.toHtmlData');
3236
this.toHtmlData(fn);
@@ -35,8 +39,8 @@
3539
api.log('FileAPI.Form.toMultipartData');
3640
this.toMultipartData(fn);
3741
}
38-
else if( api.support.chunked && options.chunkSize > 0 ){
39-
api.log('FileAPI.Form.toMultipartData');
42+
else if( options._chunked ){
43+
api.log('FileAPI.Form.toPlainData');
4044
this.toPlainData(fn);
4145
}
4246
else {
@@ -83,9 +87,29 @@
8387
if( file.file ){
8488
data.type = file.file;
8589
}
86-
data.name = file.blob.name;
87-
data.file = file.blob;
88-
data.size = file.blob.size;
90+
if( file.blob.toBlob ){
91+
// canvas
92+
queue.inc();
93+
file.blob.toBlob(function (blob){
94+
data.name = file.name;
95+
data.file = blob;
96+
data.size = blob.length;
97+
queue.next();
98+
}, 'image/png');
99+
}
100+
else if( file.file ){
101+
//file
102+
data.name = file.blob.name;
103+
data.file = file.blob;
104+
data.size = file.blob.size;
105+
}
106+
else {
107+
// additional data
108+
if (!data.params) {
109+
data.params = [];
110+
}
111+
data.params.push(encodeURIComponent(file.name) + "=" + encodeURIComponent(file.blob));
112+
}
89113
data.start = 0;
90114
data.end = 0;
91115
data.retry = 0;

lib/FileAPI.XHR.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@
129129
// html5
130130
xhr = _this.xhr = api.getXHR();
131131

132+
if (data.params) {
133+
url += (url.indexOf('?') < 0 ? "?" : "&") + data.params.join("&");
134+
}
135+
132136
xhr.open('POST', url, true);
133137
xhr.withCredential = "true";
134138

@@ -141,8 +145,8 @@
141145
});
142146

143147

144-
if (api.support.chunked && options.chunkSize > 0) {
145-
// resumable upload
148+
if ( options._chunked ) {
149+
// chunked upload
146150
if( xhr.upload ){
147151
// https://github.com/blueimp/jQuery-File-Upload/wiki/Fixing-Safari-hanging-on-very-high-speed-connections-%281Gbps%29
148152
xhr.upload.addEventListener('progress', api.throttle(function (/**Event*/evt){
@@ -199,7 +203,7 @@
199203
(slice = 'slice') in data.file || (slice = 'mozSlice') in data.file || (slice = 'webkitSlice') in data.file;
200204

201205
xhr.setRequestHeader("Content-Range", "bytes " + data.start + "-" + data.end + "/" + data.size);
202-
xhr.setRequestHeader("Content-Disposition", 'attachment; filename=' + data.name);
206+
xhr.setRequestHeader("Content-Disposition", 'attachment; filename=' + encodeURIComponent(data.name));
203207

204208
slice = data.file[slice](data.start, data.end + 1);
205209

0 commit comments

Comments
 (0)