Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append multiple file inputs to a FormData object using $.each?

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:

enter image description here

jsFiddle

http://jsfiddle.net/rwone/K7aMw/

like image 319
user1063287 Avatar asked Feb 20 '14 07:02

user1063287


People also ask

How do I append multiple files in FormData in react?

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.

How do you upload multiple files to FormData?

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.

How do you append files in input type file multiple before uploading?

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.

How do I append in FormData?

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.


1 Answers

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.

like image 98
Spokey Avatar answered Oct 07 '22 06:10

Spokey