34

i am using this code for upload files to a server(in html):

    <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
    <label>upload file<input type="file" name="file" id="file" /></label>
    <label><input type="submit" name="button" id="button" value="Submit" /></label></form>

It's open file browser and let me select a file,and when i press on Submit the file is sent to my server.

i wonder if there is a way to make multiple file select.

YosiFZ
  • 7,792
  • 21
  • 114
  • 221
  • 1
    See also [select multiple files in file upload in php](http://stackoverflow.com/questions/2071505/select-multiple-files-in-file-upload-in-php) – rds Jan 12 '12 at 10:12
  • There are many jQuery plugins like http://www.uploadify.com/ that allow you to accomplish multiple file upload. – Wes Crow Dec 29 '11 at 15:18

5 Answers5

72

You can use the multiple attribute for that, like this:

<input type="file" multiple />

To select multiple files you need to press the Ctrl key and click on the files you want to add.

js-coder
  • 8,134
  • 9
  • 42
  • 59
  • I've just tried and it worked in IE11. According to Microsoft, the ability to select multiple files for a single HTML INPUT TYPE=FILE field is new to HTML5, and is not supported by IE9 or earlier versions. – derloopkat May 21 '15 at 13:37
  • It will allow you to select multiple files at once but that doesn't means it will allow you to upload multiple files. You must allow it in your js ( If you have any for progress bar, ajax upload or upload status purpose ) and server side code like php, ASP. – Sajidur Rahman Jun 25 '15 at 10:21
13

Multiple File Select & Upload Using Spring Framework

In this post i describe server side and client side code for multiple file upload.

Following code is for mentioning the multipart data in appContext.xml

appContext.xml

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="20971520"/>
</bean>

Simpleupload.jsp:

script for multiple file upload:

<script type="text/javascript">
    var totalsizeOfUploadFiles = 0;
    function getFileSizeandName(input)
    {
        var select = $('#uploadTable');
        for(var i =0; i<input.files.length; i++)
        {           
            var filesizeInBytes = input.files[i].size;
            var filesizeInMB = (filesizeInBytes / (1024*1024)).toFixed(2);
            var filename = input.files[i].name;
            //alert("File name is : "+filename+" || size : "+filesizeInMB+" MB || size : "+filesizeInBytes+" Bytes");
            if(i<=4)
            {               
                $('#filetd'+i+'').text(filename);
                $('#filesizetd'+i+'').text(filesizeInMB);
            }
            else if(i>4)
                select.append($('<tr id=tr'+i+'><td id=filetd'+i+'>'+filename+'</td><td id=filesizetd'+i+'>'+filesizeInMB+'</td></tr>'));
            totalsizeOfUploadFiles += parseFloat(filesizeInMB);
            $('#totalsize').text(totalsizeOfUploadFiles.toFixed(2)+" MB");
            if(i==0)
                $('#filecount').text("1file");
            else
            {
                var no = parseInt(i) + 1;
                $('#filecount').text(no+"files");
            }                       
        }           
    }

    function CloseAndRefresh() 
    {
        opener.location.reload(true);
        self.close();
    }       
</script>

html form design:

<body>
<form method="post" id="uploadForm" action="upload" enctype="multipart/form-data">
    <table class="span10">
        <tr>
            <td colspan="3">
                <legend>Simple Upload</legend>
            </td>
        </tr>
        <tr>
            <td>
                <input type="file" name="files[]" multiple="multiple" onchange="getFileSizeandName(this);"/>
            </td>
        </tr>
        <tr>    
            <td colspan="3">
                <div id="uploaddiv">
                    <table id="uploadTable" class="table table-striped table-bordered">
                        <tr>
                            <th>Title</th>
                            <th>Size</th>
                        </tr>
                        <tbody id="tbodyid">
                            <tr id="tr0">
                                <td id="filetd0" height="10px" width="50px"></td>
                                <td id="filesizetd0" height="10px" width="5px"></td>
                            </tr>
                            <tr id="tr1">
                                <td id="filetd1"></td>
                                <td id="filesizetd1"></td>
                            </tr>
                            <tr id="tr2">
                                <td id="filetd2"></td>
                                <td id="filesizetd2"></td>
                            </tr>
                            <tr id="tr3">
                                <td id="filetd3"></td>
                                <td id="filesizetd3"></td>
                            </tr>
                            <tr id="tr4">
                                <td id="filetd4"></td>
                                <td id="filesizetd4"></td>
                            </tr>                                           
                        </tbody>
                        <tfoot>
                            <tr>
                                <td id="filecount"></td><td id="totalsize"></td>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </td>
        </tr>
        <tr>
            <td colspan="3">
                <button class="btn btn-primary" type="submit" id="startButton" onClick="CloseAndRefresh();">Start</button>
                <button class="btn" id="cancelButton">Cancel</button>
            </td>   
        </tr>
    </table>
</form>

UploadController.java code:

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public void UploadReceipts(@RequestParam("files[]") List<MultipartFile> file) throws Exception {
    logger.info(" Inside the upload receipts method "+file.size());
    for(int i=0; i< file.size(); i++)
    {
        if(!file.get(i).isEmpty())
        {
            CommonsMultipartFile cm = (CommonsMultipartFile) file.get(i);
            logger.info(" Inside the file upload method "+cm.getOriginalFilename());
            simpleUploadService.uploadFileandSaveReceipt(cm);
        }
    }   
}
lk.annamalai
  • 403
  • 7
  • 14
6

if using multiple file upload at form submit

<input type="file" name="file[]" multiple>

it create an array of files and can get files name easily from that array.

Sach
  • 554
  • 5
  • 14
  • 4
    Usage of the bracket notation is dependent on what you have running server-side, and is not standard. In fact, the standard way to send in duplicate form fields with the same name is to just send them in without brackets. PHP, Rails, and others decided to do things non-standard. – Brad May 29 '16 at 20:40
2

The easiest way is to layout the fields directly, like this:

<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
<label>upload file<input type="file" name="file[]" id="file1" /></label>
<label>upload file<input type="file" name="file[]" id="file2" /></label>
<label>upload file<input type="file" name="file[]" id="file3" /></label>
<label><input type="submit" name="button" id="button" value="Submit" /></label></form>

Read this on how to handle the files on server side.

However, if you want something better looking you should take a look at uploadify.

** Regarding @dotwebs answer, the multiple attribute is not supported by some browsers.

Magnus
  • 417
  • 4
  • 18
1

you may add a multiple attribute like this:

<input type="file" multiple="true" />