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

北慕城南 提交于 2019-11-27 02:51:27

问题


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/


回答1:


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.




回答2:


//your files should be doing something here
var files = yourfileInput[type = file].files

//will upload
var formData = new FormData();
//other info request takes
formData.append("otherInfoKey", $("#otherInfoID").val())
//Now turn to files
for (var i = 0; i < files.length; i++) {
    formData.append(files[i].name, files[i])
}

//Request
var xhr = new XMLHttpRequest();
xhr.open("post", "/upload", true);
xhr.upload.onprogress = function(ev) {
    var percent = 0;
    if (ev.lengthComputable) {
       percent = 100 * ev.loaded / ev.total;
       //$("#yourprogress").width(percent + "%");
       //or something like progress tip
    }
}
xhr.onloadend = function(e) {
    if (!/^2\d*$/.test(this.status)) {
         alert(this.responseText)
    }
}
xhr.onload = function(oEvent) {
    if (xhr.status == 200) {
        //go on
    }
}
xhr.send(formData);


来源:https://stackoverflow.com/questions/21901528/how-to-append-multiple-file-inputs-to-a-formdata-object-using-each

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!