Sunday, March 8, 2015

Upload multiple files using Fileupload control in

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


In earlier version of (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:
            <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>

C# Code:
using System.IO;
protected void Button1_Click(object sender, EventArgs e)
            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>";

VB Code:
Imports System.IO

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            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)
                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>"

    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.

No comments:

Post a Comment