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/
How to Upload Multiple Files in React using FormData. When we need to upload multiple files using Fetch, we have to use a new type of object called FormData. FormData allows us to append multiple key/value pairs onto the object. After we're done appending, we then pass it to the POST request's body.
Uploading Multiple Files addEventListener('change', (e) => { fileUpload(input. files); }); // Function that handles file upload using XHR const fileUpload = (files) => { // Create FormData instance const fd = new FormData(); // Iterate over all selected files Array. from(files). forEach(file => { fd.
You could add a new <input type="file"> whenever you finished uploading the previous files and hide the previous input. This way you keep adding a new input every time you want to add more files and prevent the previous input from being overwritten. And it doesn't require you to use AJAX.
FormData Methodsappend(name, value) – add a form field with the given name and value , formData. append(name, blob, fileName) – add a field as if it were <input type="file"> , the third argument fileName sets file name (not form field name), as it were a name of the file in user's filesystem, formData.
There is no good way to see the contents of FormData. A trick would be to send it (POST) and look at the network status.
Example: http://jsfiddle.net/K7aMw/2/
$(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);
});
var xhr = new XMLHttpRequest;
xhr.open('POST', '/echo/html/', true);
xhr.send(myFormData);
});
Then in the Network tab (F12) you'll see the added files when inspecting the headers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With