Single and Multiple file upload in asp.net

Single and Multiple file upload in asp.net

Code sample for single and multiple file upload in asp .net with c#

I'm going to share the code sample for single and multiple files upload in asp.net and c#


     




Table of Contents
     
1. Single file upload
2. Multiple file upload


1st step - code sample for single file and multiple file upload .aspx page.
2nd step - code sample for single file upload .aspx.cs page.
3rd step - code sample for multiple file upload .aspx.cs page.

In the 1st step

i'm going to share the code for .aspx and display view to select to upload to single and multiple files.


<fieldset>
    <legend>Click to Upload Videos Files</legend>
        <asp:UpdatePanel ID="UpdatePanel2" runat="server">
            <Triggers >
                <asp:PostBackTrigger ControlID ="btnUploadVideofiles" />
            </Triggers>
            <ContentTemplate >
               <asp:FileUpload ID="FileUploadVideofiles" runat="server" />
               <asp:Button ID="btnUploadVideofiles" runat="server" Text="Upload Video Files" onclick="btnUploadVideofiles_Click" />
               <asp:Label ID="lblMessage" runat="server" ForeColor="#3366FF"></asp:Label>
            </ContentTemplate>
        </asp:UpdatePanel>      
  </fieldset>


In the 2nd step

i'm going to share the code sample for .cs page to select to single file and click on upload button to upload the file.

 
/// <summary>

/// Click to upload single file.
/// </summary>
protected void btnUploadVideofiles_Click(object sender, EventArgs e)
{
    if (FileUploadVideofiles.HasFile)
    {
        try
        {
            string filename = Path.GetFileName(FileUploadVideofiles.FileName);
            string fileExtension = Path.GetExtension(filename);
            if (fileExtension == ".wmv" || fileExtension == ".swf" || fileExtension == ".avi" || fileExtension == ".mp4" || fileExtension == ".mpeg" || fileExtension == ".mpg" || fileExtension == ".qt" || fileExtension == ".flv")
            {
                FileUploadVideofiles.SaveAs(Server.MapPath(@"~/Upload/Media/") + filename);
                string filePath = @"~/Upload/Media/" + filename;
                ClassManager objDataM = new ClassManager();
                objDataM.insertVideoFiles(filePath, filename, DateTime.Now);
                lblMessage.Text = "File is uploaded!";
            }
            else
            {
                lblMessage.Text = "Please Upload videos file only!";
            }
        }
        catch (Exception ex)
        {
            lblMessage.Text = "The file could not be uploaded. The following error occured: " + ex.Message;
        }
    }
      
      
In The 3rd step
also i'm going to share the code sample for .cs page to select multiple files upload and click to upload button to upload files.


/// <summary>
/// click to upload multiple files.
/// </summary>
protected void btnUploadVideofiles_Click(object sender, EventArgs e)
{
    if (FileUploadVideofiles.HasFile)
    {
        try
        {
            // Get the HttpFileCollection
            HttpFileCollection uploadedVideoFiles = Request.Files;
            for (int i = 0; i < uploadedVideoFiles.Count; i++)
            {
                HttpPostedFile hpfiles = uploadedVideoFiles[i];
                if (hpfiles.ContentLength > 0)
                {
                    hpfiles.SaveAs(Server.MapPath("~/Upload/Media/") + Path.GetFileName(hpfiles.FileName));
                }
            }
        }
        catch (Exception ex)
        {
            lblMessage.Text = "The file could not be uploaded. The following error occured: " + ex.Message;
        }
    }
}



Thanks you!!!
If you happy for this post then leave a comment.

ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

Single and Multiple file upload in asp.net Single and Multiple file upload in asp.net Reviewed by Unknown on 3:14 AM Rating: (5)
www.code-sample.com/. Powered by Blogger.
^