使用AllowMultiple = true

时间:2017-05-20 17:29:17

标签: c# asp.net file-upload webforms httppostedfile

我有一个<asp:FileUpload>控件,其属性AllowMultiple设置为true:

<asp:fileupload id="FileUpload" runat="server" allowmultiple="true" cssclass="fileUpload btn btn-sm btn-default" onchange="preloadImages()" />

<div class="field col-md-2 col-md-offset-1 immScarpePreview MainPreviewBox">
 <asp:image id="Image1" runat="server" visible="false" cssclass="img-responsive" />
 <asp:button id="ImmButton" runat="server" text="Upload" onclick="ImmButton_Click" cssclass="hidden" />
</div>

在我的JavaScript函数中,我模拟了对隐形按钮的点击:

<script>
    function preloadImages() {
        $('#<%=ImmButton.ClientID%>').trigger('click');
    }
</script>

在代码隐藏中,我将文件保存到临时文件夹,然后在<asp:Image>控件中显示上传的图像:

 protected void ImmButton_Click(object sender, EventArgs e)
 {
        if (FileUpload.HasFile)
        {
            try
            {
                int cont = 0;
                byte[] fileData = null;
                foreach (HttpPostedFile file in FileUpload.PostedFiles)
                {
                    if (cont == 0)
                    {
                        using (var binaryReader = new BinaryReader(file.InputStream))
                            fileData = binaryReader.ReadBytes(file.ContentLength);
                        File.WriteAllBytes(Server.MapPath("immScarpe/tmp/" + file.FileName), fileData);
                        setImage1("immScarpe/tmp/" + file.FileName);
                    }
                    else if (cont == 1)
                    {
                        using (var binaryReader = new BinaryReader(file.InputStream))
                            fileData = binaryReader.ReadBytes(file.ContentLength);
                        File.WriteAllBytes(Server.MapPath("immScarpe/tmp/" + file.FileName), fileData);
                        setImage2("immScarpe/tmp/" + file.FileName);
                    }
                    else if (cont == 2)
                     //and so on...

                    //so on...
                    cont++;
                }
            }
            catch(Exception ex)
            {
                //error writing file
                Console.Write(ex.Message);
            }
        }
 }

 private void setImage1(string image) //all equals for all the 5 images
 {
     Image1.ImageUrl = image;
     Image1.Visible = true;
 }

这很完美但我需要一些帮助。当我遍历FileUpload.PostedFiles时,我认为所选图像的顺序是按字母顺序排列的。我想保留用户选择的顺序。这可能吗?

1 个答案:

答案 0 :(得分:2)

我有坏消息,至少如果你使用的是Windows。

我在Windows 10上使用Chrome,Edge,Firefox和Internet Explorer测试了您的代码,我使用Telerik Fiddler检查了HTTP POST请求。我在所有浏览器中得到了相同的结果:

  • FileUpload.PostedFiles集合中的文件顺序与HTTP请求中的相同; ASP.NET不会对集合中的文件进行排序。
  • HTTP请求中的文件顺序与上传控件中的相同;浏览器不对HTTP请求中的文件进行排序。 (要查看上传控件中的文件,我已禁用您的preloadImages功能。)
  • 上传控件(以及客户端files集合)中的文件顺序必须与文件名相同> Windows文件选择对话框中的框:
    • 如果我不按顺序手动输入文件名,那么上传控件按照我输入的顺序列出文件,FileUpload.PostedFiles集合维护该顺序。
    • 但是,如果按Ctrl +单击选择文件乱序,则上传控件会按字母顺序列出文件,FileUpload.PostedFiles集合也是如此。

在最后一种情况下,我假设Windows在将文件提供给浏览器之前对文件进行排序。如果这是真的,那么浏览器永远不会收到用户选择的原始订单,因此您无法维护订单,即使使用JavaScript也是如此。

如果您绝对必须维护多个上传文件的顺序,那么您可能需要拥有多个<asp:FileUpload>控件,每个控件都带有AllowMultiple="False"