diff --git a/lib/FileAPI.Form.js b/lib/FileAPI.Form.js index 050ce3b7..923e9e74 100644 --- a/lib/FileAPI.Form.js +++ b/lib/FileAPI.Form.js @@ -27,6 +27,10 @@ }, 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); @@ -35,8 +39,8 @@ api.log('FileAPI.Form.toMultipartData'); this.toMultipartData(fn); } - else if( api.support.chunked && options.chunkSize > 0 ){ - api.log('FileAPI.Form.toMultipartData'); + else if( options._chunked ){ + api.log('FileAPI.Form.toPlainData'); this.toPlainData(fn); } else { @@ -83,9 +87,29 @@ if( file.file ){ data.type = file.file; } - data.name = file.blob.name; - data.file = file.blob; - data.size = file.blob.size; + if( file.blob.toBlob ){ + // canvas + queue.inc(); + file.blob.toBlob(function (blob){ + data.name = file.name; + data.file = blob; + data.size = blob.length; + queue.next(); + }, 'image/png'); + } + else if( file.file ){ + //file + data.name = file.blob.name; + data.file = file.blob; + data.size = file.blob.size; + } + else { + // additional data + if (!data.params) { + data.params = []; + } + data.params.push(encodeURIComponent(file.name) + "=" + encodeURIComponent(file.blob)); + } data.start = 0; data.end = 0; data.retry = 0; diff --git a/lib/FileAPI.XHR.js b/lib/FileAPI.XHR.js index 1dd17224..abc51458 100644 --- a/lib/FileAPI.XHR.js +++ b/lib/FileAPI.XHR.js @@ -129,6 +129,10 @@ // html5 xhr = _this.xhr = api.getXHR(); + if (data.params) { + url += (url.indexOf('?') < 0 ? "?" : "&") + data.params.join("&"); + } + xhr.open('POST', url, true); xhr.withCredential = "true"; @@ -141,8 +145,8 @@ }); - if (api.support.chunked && options.chunkSize > 0) { - // resumable upload + if ( options._chunked ) { + // chunked upload if( xhr.upload ){ // https://github.com/blueimp/jQuery-File-Upload/wiki/Fixing-Safari-hanging-on-very-high-speed-connections-%281Gbps%29 xhr.upload.addEventListener('progress', api.throttle(function (/**Event*/evt){ @@ -199,7 +203,7 @@ (slice = 'slice') in data.file || (slice = 'mozSlice') in data.file || (slice = 'webkitSlice') in data.file; xhr.setRequestHeader("Content-Range", "bytes " + data.start + "-" + data.end + "/" + data.size); - xhr.setRequestHeader("Content-Disposition", 'attachment; filename=' + data.name); + xhr.setRequestHeader("Content-Disposition", 'attachment; filename=' + encodeURIComponent(data.name)); slice = data.file[slice](data.start, data.end + 1);