admin管理员组文章数量:1300045
It seems like the Newly added FormData
class in Javascript is getting rampant on line. Mostly when targeting multiple file uploads with Ajax. But some how it has some patibility issues with most of IE if not 10+...
Should I have an HTML like:
<form id="normalForm" enctype="multipart/form-data" onSubmmit="gaga()">
<input name="aFile" id="aFile" type="file" multiple/>
<input type="button" value="go">
</form>
and the normal javaScript:
function gaga {
var f= document.getElementById("normalForm");
/// do something
}
or the a function with the New FormData:
function nGaga (){
var f= new FormData()
f.append("aFile", fileInputElement.files[0])
/// and whatever else to be appended
}
After going through some reading, i somehow learnt that, this is mostly used to generate "Key:value"
object in Javascript. However, using jQuery, I could do somethinglike:
var f= $('#normalForm').serializeArray();
This will kinda give me the "Key:value"
object.
So, why should one use the new FormData
while dealing with XMLHTTPrequests despite it's issues? and what is the difference?
It seems like the Newly added FormData
class in Javascript is getting rampant on line. Mostly when targeting multiple file uploads with Ajax. But some how it has some patibility issues with most of IE if not 10+...
Should I have an HTML like:
<form id="normalForm" enctype="multipart/form-data" onSubmmit="gaga()">
<input name="aFile" id="aFile" type="file" multiple/>
<input type="button" value="go">
</form>
and the normal javaScript:
function gaga {
var f= document.getElementById("normalForm");
/// do something
}
or the a function with the New FormData:
function nGaga (){
var f= new FormData()
f.append("aFile", fileInputElement.files[0])
/// and whatever else to be appended
}
After going through some reading, i somehow learnt that, this is mostly used to generate "Key:value"
object in Javascript. However, using jQuery, I could do somethinglike:
var f= $('#normalForm').serializeArray();
This will kinda give me the "Key:value"
object.
So, why should one use the new FormData
while dealing with XMLHTTPrequests despite it's issues? and what is the difference?
1 Answer
Reset to default 9Say for instance that you wanted to upload a PDF but also needed to include some user generated metadata along with it as JSON or XML (or anything really). With FormData not only can we upload Files and strings (which is already possible with HTML forms) but we can also upload Blobs which allow us to upload dynamically generated content and be able to specify the content-type:
document.getElementById('dropzone').ondrop = function(e){
e.preventDefault();
uploadFiles(e.dataTransfer.files);
};
function uploadFiles(files) {
var nextId = 123;
var xml = "<a id=\"a\"><b id=\"b\">hey!<\/b><\/a>";
var json = {
title: "Hello World!",
tags: ["pdf", "non-fiction", "literature"],
token: "ABCeasyAs123"
};
files.forEach(function(file, i) {
var formData = new FormData();
var xhr = new XMLHttpRequest();
json.id = nextId + i;
formData.append("XML", new Blob([ xml ], { type: "text/xml" });
formData.append("JSON", new Blob([ JSON.stringify(json) ], { type: "application/json" }));
formData.append("PDF", file);
xhr.open("POST", "/upload");
xhr.upload.onprogress = function(event) {
if (event.lengthComputable) {
var progress = (event.loaded / event.total * 100 | 0);
console.log('Upload #%d is %d% plete.', i+1, progress);
}
}
xhr.send(formData);
});
}
版权声明:本文标题:jquery - Difference between Javascript new FormData and HTML form with (form).serializeArray()? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741614241a2388436.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论