From b07905e24202d6caaa23930f41dfa6d9e59089c9 Mon Sep 17 00:00:00 2001 From: Ilya Lebedev Date: Mon, 11 Feb 2013 15:18:10 +0400 Subject: [PATCH 1/3] added filename escaping for Content-Disposition header --- lib/FileAPI.XHR.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/FileAPI.XHR.js b/lib/FileAPI.XHR.js index 1dd17224..1610daa3 100644 --- a/lib/FileAPI.XHR.js +++ b/lib/FileAPI.XHR.js @@ -199,7 +199,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); From 52842ceb46bb0334b4769759c646622269ddea9c Mon Sep 17 00:00:00 2001 From: Ilya Lebedev Date: Mon, 11 Feb 2013 15:57:11 +0400 Subject: [PATCH 2/3] fixed log message --- lib/FileAPI.Form.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/FileAPI.Form.js b/lib/FileAPI.Form.js index 050ce3b7..4af03c4a 100644 --- a/lib/FileAPI.Form.js +++ b/lib/FileAPI.Form.js @@ -36,7 +36,7 @@ this.toMultipartData(fn); } else if( api.support.chunked && options.chunkSize > 0 ){ - api.log('FileAPI.Form.toMultipartData'); + api.log('FileAPI.Form.toPlainData'); this.toPlainData(fn); } else { From e88e488d5729dcfb9b07e8483dcbe5cfe3862112 Mon Sep 17 00:00:00 2001 From: Ilya Lebedev Date: Mon, 11 Feb 2013 18:46:36 +0400 Subject: [PATCH 3/3] added checks for multiple files in a single request and additional request params --- lib/FileAPI.Form.js | 32 ++++++++++++++++++++++++++++---- lib/FileAPI.XHR.js | 8 ++++++-- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/lib/FileAPI.Form.js b/lib/FileAPI.Form.js index 4af03c4a..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,7 +39,7 @@ api.log('FileAPI.Form.toMultipartData'); this.toMultipartData(fn); } - else if( api.support.chunked && options.chunkSize > 0 ){ + else if( options._chunked ){ api.log('FileAPI.Form.toPlainData'); this.toPlainData(fn); } @@ -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 1610daa3..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){