Skip to content

Commit

Permalink
修改FormDataShim多次上传的问题
Browse files Browse the repository at this point in the history
FormDataShim 在做选择多个文件,多次上传,后面上传会出问题,
主要因为XMLHttpRequest.prototype.send = oldSend 没处理多个FormDataShim 实例的问题。
  • Loading branch information
zuoge85 committed Apr 7, 2016
1 parent 9f759fe commit 4ca0f0c
Showing 1 changed file with 66 additions and 59 deletions.
125 changes: 66 additions & 59 deletions src/lib/Blob.FormData.shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,65 +32,72 @@ function hasFormDataBug () {
// QQ X5浏览器也有这个BUG
return bCheck && navigator.userAgent.match(/AppleWebKit\/(\d+)/).pop() <= 534 || /MQQBrowser/g.test(navigator.userAgent);
}
function FormDataShim () {
var
// Store a reference to this
o = this,

// Data to be sent
parts = [],

// Boundary parameter for separating the multipart values
boundary = Array(21).join('-') + (+new Date() * (1e16 * Math.random())).toString(36),

// Store the current XHR send method so we can safely override it
oldSend = XMLHttpRequest.prototype.send;
this.getParts = function () {
return parts.toString();
};
this.append = function (name, value, filename) {
parts.push('--' + boundary + '\r\nContent-Disposition: form-data; name="' + name + '"');

if (value instanceof Blob) {
parts.push('; filename="' + (filename || 'blob') + '"\r\nContent-Type: ' + value.type + '\r\n\r\n');
parts.push(value);
}
else {
parts.push('\r\n\r\n' + value);
}
parts.push('\r\n');
};

XMLHttpRequest.prototype.send = function (val) {
var fr,
data,
oXHR = this;

if (val === o) {
// Append the final boundary string
parts.push('--' + boundary + '--\r\n');
// Create the blob
data = new BlobConstructor(parts);

// Set up and read the blob into an array to be sent
fr = new FileReader();
fr.onload = function () {
oldSend.call(oXHR, fr.result);
};
fr.onerror = function (err) {
throw err;
};
fr.readAsArrayBuffer(data);

// Set the multipart content type and boudary
this.setRequestHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
XMLHttpRequest.prototype.send = oldSend;
}
else {
oldSend.call(this, val);
}
};
}
var FormDataShim=(function(){
var formDataShimNums = 0;
return function FormDataShim () {
var
// Store a reference to this
o = this,

// Data to be sent
parts = [],

// Boundary parameter for separating the multipart values
boundary = Array(21).join('-') + (+new Date() * (1e16 * Math.random())).toString(36),

// Store the current XHR send method so we can safely override it
oldSend = XMLHttpRequest.prototype.send;
this.getParts = function () {
return parts.toString();
};
this.append = function (name, value, filename) {
parts.push('--' + boundary + '\r\nContent-Disposition: form-data; name="' + name + '"');

if (value instanceof Blob) {
parts.push('; filename="' + (filename || 'blob') + '"\r\nContent-Type: ' + value.type + '\r\n\r\n');
parts.push(value);
}
else {
parts.push('\r\n\r\n' + value);
}
parts.push('\r\n');
};

formDataShimNums++;
XMLHttpRequest.prototype.send = function (val) {
var fr,
data,
oXHR = this;

if (val === o) {
// Append the final boundary string
parts.push('--' + boundary + '--\r\n');
// Create the blob
data = new BlobConstructor(parts);

// Set up and read the blob into an array to be sent
fr = new FileReader();
fr.onload = function () {
oldSend.call(oXHR, fr.result);
};
fr.onerror = function (err) {
throw err;
};
fr.readAsArrayBuffer(data);

// Set the multipart content type and boudary
this.setRequestHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
formDataShimNums--;
if(formDataShimNums == 0){
XMLHttpRequest.prototype.send = oldSend;
}
}
else {
oldSend.call(this, val);
}
};
}
})();


module.exports = {
Expand Down

0 comments on commit 4ca0f0c

Please sign in to comment.