Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 uploaded one by one by clicking browse button, and click submit button. But I need to allow users to select multiple files at the same time.

like image 239
priceline Avatar asked Apr 01 '11 02:04

priceline


People also ask

Can you upload multiple files at once?

Answer: Yes, dragging and dropping multiple files into the Documents area or using the New > Upload Files options will allow you to select more than one file at a time to be uploaded.

What is IFormFile C#?

What is IFormFile. ASP.NET Core has introduced an IFormFile interface that represents transmitted files in an HTTP request. The interface gives us access to metadata like ContentDisposition, ContentType, Length, FileName, and more. IFormFile also provides some methods used to store files.


2 Answers

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.

<input type="file" id="flAttachment" runat="server" tabindex="8" class="multi max-3 accept-gif|jpg|xlsx|xls|doc|docx|pdf|png" size="37" />

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.

like image 165
Devraj Gadhavi Avatar answered Sep 22 '22 08:09

Devraj Gadhavi


Set the property "AllowMultiple = true" as below. This property is available for 4.5 framework.

 <asp:FileUpload ID="file_upload" runat="server" AllowMultiple="true" />

This will allow you to select multiple files at one time

Aspx Code:

<form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="file_upload" runat="server" AllowMultiple="true" />
        <asp:Button ID="btnFileUpload" runat="server" Text="Upload" OnClick="btnFileUpload_Click" />
        <asp:Label ID="lblUploadStatus" runat="server"></asp:Label>
    </div>
</form>

Aspx.cs Code:

protected void btnFileUpload_Click(object sender, EventArgs e)
{
    try
    {
        if (file_upload.HasFile && file_upload.PostedFiles.All(x => x.ContentType == "image/jpeg" && x.ContentLength < 102400))
        {
            foreach (var file in file_upload.PostedFiles)
            {
                file_upload.SaveAs(Server.MapPath("~/") + Path.GetFileName(file.FileName));
            }
            lblUploadStatus.Text = "File(s) uploaded successfully.";
        }
        else
        {
            lblUploadStatus.Text = "Please upload proper file.";
        }
    }
    catch (Exception ex)
    {
        lblUploadStatus.Text = "Error in uploading file." + ex.Message;
    }
}
like image 29
Shreya Avatar answered Sep 18 '22 08:09

Shreya