Created
January 26, 2017 18:37
-
-
Save rbarros/15c3fd46628ce38d4feb1923bcf0554b to your computer and use it in GitHub Desktop.
Emulate FormData for some browsers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Emulate FormData for some browsers | |
* MIT License | |
* (c) 2010 François de Metz | |
*/ | |
/* jslint devel: true, unparam: true, quotmark: double, indent: 2 */ | |
(function(w) { | |
"use strict"; | |
if (w.FormData) { | |
return; | |
} | |
function FormData() { | |
this.fake = true; | |
this.boundary = "--------FormData" + Math.random(); | |
this._fields = []; | |
} | |
FormData.prototype.append = function(key, value) { | |
this._fields.push([key, value]); | |
}; | |
FormData.prototype.toString = function() { | |
var boundary = this.boundary; | |
var body = ""; | |
this._fields.forEach(function(field) { | |
body += "--" + boundary + "\r\n"; | |
// file upload | |
if (field[1].name) { | |
var file = field[1]; | |
body += "Content-Disposition: form-data; name=\""+ field[0] +"\"; filename=\""+ file.name +"\"\r\n"; | |
body += "Content-Type: "+ file.type +"\r\n\r\n"; | |
body += file.getAsBinary() + "\r\n"; | |
} else { | |
body += "Content-Disposition: form-data; name=\""+ field[0] +"\";\r\n\r\n"; | |
body += field[1] + "\r\n"; | |
} | |
}); | |
body += "--" + boundary +"--"; | |
return body; | |
}; | |
w.FormData = FormData; | |
}(this)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very good, my friend @rbarros.