Multi file upload using c# on ASP.NET 4.0 environment

后端 未结 6 2051
死守一世寂寞
死守一世寂寞 2020-12-30 08:49

I am looking for a solution to upload multiple files (click on browse button, and select multiple files using shift key).

I see several solutions that need to be upl

6条回答
  •  [愿得一人]
    2020-12-30 09:20

    We have been using the below jQuery plugin to help us.

    jQuery Multiple File Upload Plugin

    After including the necessary js file : jQuery.multifile.pack.js, we can use it like below.

    
    

    Giving class="multi" makes it to accept more than one file.

    You can also apply the constraints if you want. Like class = "max-3" would allow maximum three files to be uploaded. class = "accept-gif|jpg" would only allow files with gif OR jpg extensions to be uploaded.

    For getting the multiple files on the sever side you will need to include the namespace : System.Web;

    Then you can have the below code for iterating through each file uploaded.

    if (Request.Files.Count > 0)
    {
        HttpFileCollection attachments = Request.Files;
        for (int i = 0; i < attachments.Count; i++)
        {
            HttpPostedFile attachment = attachments[i];
            if (attachment.ContentLength > 0 && !String.IsNullOrEmpty(attachment.FileName))
            {
                //do your file saving or any related tasks here.
            }
        }
    }
    

    This would be regardless of .net framework version.

提交回复
热议问题