FormData support where window.FormData is undefined
This is project was inspired by html5-formdata and it implements FormData
with append
and toString
methods.
But unlike html5-formdata, it supports Blob
and use FileReader
and ArrayBuffer
to convert Blob
to string.
npm install formdata-emulate
import formdata.js:
<script type="text/javascript" src="formdata.js"></script>
use XMLHttpRequest.
var formData = new FormData();
formData.append("username", "sam");
// HTML file input, chosen by user
formData.append("userfile", document.querySelector("#file").files[0]);
// JavaScript file-like object
var content = '<a id="a"><b id="b">hey!</b></a>'; // the body of the new file...
var blob = new Blob([content], { type: "text/xml"});
formData.append("webmasterfile", blob);
var xhr = new XMLHttpRequest();
xhr.open("POST", "");
if (formData.polyfill) {
xhr.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + formData.boundary)
// formData.toString() returns Promise
formData.toString().then(function(data){
xhr.send(data);
})
} else {
// normal way
xhr.send(formData);
}
use with fetch.
var postData = function(formData){
if (formData.polyfill) {
return formData.toString().then(function(data){
return fetch("", {
method: "POST",
headers: {
"Content-Type": "multipart/form-data; boundary=" + formData.boundary
},
body: data
})
})
} else {
return fetch("", {
method: "POST",
body: datda
})
}
}
postData(formData).then(function(res){
console.log(res)
});
See examples in examples directory
This project was inspired by these good projects