TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

ASP.Net Upload Multiple Files

ASP.Net Upload Multiple Files with asp.net tutorial, asp.net introduction, features, project, example, server controls, labels, textbox, button, hyperlink, radiobutton, calender, checkbox, fileupload, events handling, authentication, webforms model binding, html server control, compare validdator, range validator, validation summary, mvc introduction, mvc project, view, validation, entity framework, authentication etc.

<< Back to ASP

ASP.NET Upload Multiple Files

ASP.NET FileUpload control provides AllowMultiple property to upload multiple files to the server. This property takes either true or false value.

The <asp:FileUpload> tag is used to create a browse button that allows us to upload file. Let?s create an example to upload multiple files.

ASP.NET Upload Multiple Files Example

This example contains the following files.

// UploadMultipleFilesExample.aspx

<%@ Page Language="C#" AutoEventWireup="true" 
CodeBehind="UploadMultipleFilesExample.aspx.cs" Inherits="UploadMultipleExample.UploadMultipleFilesExample" %>
<!DOCTYPE html>  
    <html xmlns="http://www.w3.org/1999/xhtml">  
    <head runat="server">  
        <title></title>  
    </head>  
    <body>  
        <form id="form1" runat="server">  
            <div>  
                <h3>Upload Multiple Files</h3>  
                <asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="true" />  
            </div>  
            <p>  
                <asp:Button ID="Button1" runat="server" Text="Upload File" OnClick="Button1_Click" />  
            </p>  
        </form>  
        <p>  
            <asp:Label runat="server" ID="FileUploadStatus"></asp:Label>  
        </p>  
    </body>  
    </html>  

// UploadMultipleFilesExample.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace UploadMultipleExample
{
    public partial class UploadMultipleFilesExample : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if ((FileUpload1.PostedFile != null) && (FileUpload1.PostedFile.ContentLength > 0))
            {
                var count = 0;
                foreach (HttpPostedFile uploadedFile in FileUpload1.PostedFiles)
                {
                    string fn = System.IO.Path.GetFileName(uploadedFile.FileName);
                    string SaveLocation = Server.MapPath("upload") + "\\" + fn;
                    try
                    {
                        uploadedFile.SaveAs(SaveLocation);
                        count++;
                    }
                    catch (Exception ex)
                    {
                        FileUploadStatus.Text = "Error: " + ex.Message;
                    }
                }
                if (count > 0)
                {
                    FileUploadStatus.Text = count + " files has been uploaded.";
                }
            }
            else
            {
                FileUploadStatus.Text = "Please select a file to upload.";
            }
        }
    }
}

Output:

ASP Net Upload Multiple File 1

Selecting 2 files to upload

ASP Net Upload Multiple File 2
ASP Net Upload Multiple File 3

See, initially, the upload folder is empty.

ASP Net Upload Multiple File 4

Uploading files to the server.

ASP Net Upload Multiple File 5

Now, look at the upload folder. It contains uploaded two files.

ASP Net Upload Multiple File 6




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf