Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing filenames of a multiple file upload form

I want to read out the filenames of my multi upload form, however Javascript is just adding the first item only.

<input name="upload[]" id="upload" type="file" multiple="multiple">
$("#upload").change(function() {
    $("#upload").each(function() {
        $("#upload_prev").append(this.value);
    });
});
like image 884
yajRs Avatar asked Apr 26 '12 09:04

yajRs


People also ask

How do I select multiple files in a form?

Tip: For <input type="file"> : To select multiple files, hold down the CTRL or SHIFT key while selecting.

How do I select multiple files using file upload control?

The FileUpload. AllowMultiple property in . NET 4.5 and higher will allow you the control to select multiple files.

What is the easiest way to upload single or multiple files with FormData?

Uploading Multiple Files const uploadFile = (files) => { console. log("Uploading file..."); const API_ENDPOINT = "https://file.io"; const request = new XMLHttpRequest(); const formData = new FormData(); request. open("POST", API_ENDPOINT, true); request. onreadystatechange = () => { if (request.


1 Answers

Multiple file upload fields are not yet very well supported by jQuery. Your best option is to revert to native javascript to get access to the files collection. Try this:

$("#upload").change(function() {
    var files = $(this)[0].files;
    for (var i = 0; i < files.length; i++) {
        $("#upload_prev").append(files[i].name);
    }
});

Example fiddle

Also, here's a fiddle with a couple of the issues with your example fixed, such as clearing the previous list of files when re-selecting and appending the filename on a new line: http://jsfiddle.net/Vs5Hk/3/

like image 165
Rory McCrossan Avatar answered Oct 13 '22 12:10

Rory McCrossan