I have a multiple (and dynamic) number of inputs of type=file
.
I want to create a FormData object out of them.
I need to manually append them to the object as I need access to their filenames for inserting into a database and therefore need to specify a filename is this format:
myFormData.append(name,file,filename);
HTML
<form id="my_form" enctype="multipart/form-data">
<input type="file" name="undefined" id="file_1" data-filename="image.jpg">
<input type="file" name="undefined" id="file_2" data-filename="image2.jpg">
<button>click</button>
</form>
jQuery
var myFormData = new FormData();
$(document).on("click", "button", function(e) {
e.preventDefault();
var inputs = $("#my_form input");
$.each(inputs,function(obj,v) {
var file = v.files[0];
var filename = $(v).attr("data-filename");
var name = $(v).attr("id");
myFormData.append(name, file, filename);
});
//alert(JSON.stringify(myFormData));
console.log(myFormData);
});
I don't think the object is being constructed properly and I haven't been able to correctly view the contents of the object to confirm this.
This is what I get in the console:
jsFiddle
http://jsfiddle.net/rwone/K7aMw/
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…