Upload multiple files using Fileupload control in asp.net - Free Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server,WCF,Entity framework snippets and tutorial

Upload multiple files using Fileupload control in asp.net

Introduction: In this article I will explain how to upload multiple files using Fileupload control in asp.net 4.5

Description:

In earlier version of Asp.net (4.0), there is no option to upload multiple files using fileupload control but now Fileupload control support the multiple files selection features and upload all the selected files at once.

Html Markup Of page:
<table>
            <tr><td>Upload Image:</td><td> <asp:fileupload ID="Fileupload1" runat="server" AllowMultiple="true"></asp:fileupload></td></tr>
            <tr><td></td><td><asp:Button ID="Button1" runat="server" Text="Upload File" OnClick="Button1_Click" /></td></tr>
        </table>

C# Code:
using System.IO;
protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (Fileupload1.HasFiles)
            {
                foreach (HttpPostedFile postedfile in Fileupload1.PostedFiles)
                {
                    string fileName = Path.GetFileName(postedfile.FileName);
                    postedfile.SaveAs(Server.MapPath("~/ImageUploads/") +Guid.NewGuid()+ fileName);
                }
                Messagebox(string.Format("{0} Files have been uploaded Successfully", Fileupload1.PostedFiles.Count));
            }
        }
        catch (Exception ex)
        {
        }
    }
    private void Messagebox(string Message)
    {
        Label lblMessageBox = new Label();
        lblMessageBox.Text =
            "<script language='javascript'>" + Environment.NewLine +
            "window.alert('" + Message + "')</script>";
        Page.Controls.Add(lblMessageBox);
    }

VB Code:
Imports System.IO

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            If Fileupload1.HasFiles Then
                For Each postedfile As HttpPostedFile In Fileupload1.PostedFiles
                    Dim fileName As String = Path.GetFileName(postedfile.FileName)
                    postedfile.SaveAs(Server.MapPath("~/ImageUploads/") & fileName)
                Next
                Messagebox(String.Format("{0} Files have been uploaded Successfully", Fileupload1.PostedFiles.Count))
            End If
        Catch ex As Exception
        End Try
    End Sub
    Private Sub Messagebox(ByVal Message As String)
        Dim lblMessageBox As New Label()
        lblMessageBox.Text = "<script language='javascript'>" + Environment.NewLine & "window.alert('" & Message & "')</script>"
        Page.Controls.Add(lblMessageBox)

    End Sub

  Is this article helpful for you?

If yes post your comment to appreciate my work and fell free to contact me. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

What do you think about this article?

If you found this article useful, please share and follow on Facebook, Twitter, Google Plus and other social media websites. To get free updates subscribe to newsletter. Please put your thoughts and feedback in comments section.

Share this

Share on FacebookTweet on TwitterPlus on Google+


EmoticonEmoticon