【问题标题】:Multiple files select and upload多个文件选择和上传
【发布时间】:2011-12-29 15:17:08
【问题描述】:

我正在使用此代码将文件上传到服务器(在 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>

它打开文件浏览器,让我选择一个文件,当我按下提交时,文件被发送到我的服务器。

我想知道是否有办法使多个文件选择。

【问题讨论】:

标签: javascript html


【解决方案1】:

您可以为此使用multiple 属性,如下所示:

<input type="file" multiple />

要选择多个文件,您需要按Ctrl 键并单击要添加的文件。

【讨论】:

  • 我刚刚尝试过,它在 IE11 中工作。根据 Microsoft 的说法,为单个 HTML INPUT TYPE=FILE 字段选择多个文件的功能是 HTML5 的新功能,IE9 或更早版本不支持。
  • 它允许您一次选择多个文件,但这并不意味着它允许您上传多个文件。您必须在您的 js 中允许它(如果您有任何用于进度条、ajax 上传或上传状态目的)和服务器端代码,如 php、ASP。
【解决方案2】:

使用 Spring 框架选择和上传多个文件

在这篇文章中,我描述了用于多个文件上传的服务器端和客户端代码。

以下代码用于提及 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 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表单设计:

<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 代码:

@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);
        }
    }   
}

【讨论】:

    【解决方案3】:

    如果在表单提交时使用多个文件上传

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

    它创建一个文件数组,并且可以从该数组中轻松获取文件名。

    【讨论】:

    • 括号符号的使用取决于您在服务器端运行的内容,并且不是标准的。事实上,发送具有相同名称的重复表单字段的标准方法是不带括号发送它们。 PHP、Rails 和其他人决定做一些非标准的事情。
    【解决方案4】:

    最简单的方法是直接布局字段,如下所示:

    <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>
    

    阅读this 了解如何在服务器端处理文件。

    但是,如果你想要更好看的东西,你应该看看uploadify

    ** 关于@dotwebs 的回答,multiple 属性是not supported by some browsers

    【讨论】:

    • 您的第一个链接已损坏。
    • 谢谢。我不记得我在想什么链接,但现在我插入了一个官方 PHP 手册的链接。
    【解决方案5】:

    您可以像这样添加多个属性:

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

    【讨论】:

    • 记得在名称中插入括号 name="file[]"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-26
    • 1970-01-01
    • 2011-09-12
    • 2011-07-28
    • 2010-11-16
    • 1970-01-01
    相关资源
    最近更新 更多