python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 7, Interview Questions Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
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

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - http://code-sample.com and http://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
www.code-sample.com/. Powered by Blogger.
ASK Questions