Click here to Skip to main content
6,630,586 members and growing! (17,198 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

Multiple File Uploader

By Govardhana Reddy

This Article will help in Uploading Multiple Files in ASP.Net Web Application
Windows, .NET 2.0, ASP.NET, WebForms, VS.NET2003, VS2005, Dev
Posted:28 Sep 2007
Views:11,305
Bookmarked:27 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
4 votes for this article.
Popularity: 1.26 Rating: 2.09 out of 5
2 votes, 50.0%
1
1 vote, 25.0%
2

3

4
1 vote, 25.0%
5
Screenshot - MultiFile_Uploader_640_X_800.jpg

Introduction

There are many situations when a user wants to allow a user to upload more than one file, you're stuck with either adding as many file input elements as the number of files you want to upload, or possibly having new ones appear 'magically' through Javascript.

Background

This article helps in uploading multiple files simultaneously in ASP.Net . This article uses HTML File Input Control and Grid View.

Using the code

Using this article doesn't get much simpler. Thanks to Microsoft's DotNet.

The heart of this article is the script which helps to get the FileOpen Dialog in ASP.Net

<script type="text/javascript"  language="javascript">
     
       function getFile()
        {
           document.getElementById("file1").click();
           
           var file = "";
           
           if(document.getElementById("TextBox1").value == "")
                file = document.getElementById("file1").value; 
           else
                file = document.getElementById("TextBox1").value + "\r\n" + document.getElementById("file1").value;
           document.getElementById("TextBox1").value = file ;
        }

</script>

To use this article. just run the web application, once done its easy just click the Add Files Button to start uploading the files the file along with the details is shown in the Grid View.

The Code for the Add Files Button Click is shown below :

 protected void Button1_Click(object sender, EventArgs e)
    {
     
        if (TextBox1.Text.Length > 0)
        {
            DataTable dt;
            DataRow dr = null;


            FileInfo fileObj = new FileInfo(TextBox1.Text.Trim());

            long size = fileObj.Length / 1024;

            loggedUser = "Administrator";

            folderPath = System.Web.Hosting.HostingEnvironment.MapPath("~/" + loggedUser);

            System.IO.DirectoryInfo dirObj = new DirectoryInfo(folderPath);

            if (!dirObj.Exists)
                dirObj.Create();

            try
            {

                fileObj.CopyTo(folderPath + "\\" + fileObj.Name);
            }
            catch (Exception ee)
            {
                TextBox1.Text = "";
                string error = ee.Message.ToString();
                Response.Write("<script> window.alert(' File With the same name already uploaded ')</script>");
                return;
            }

            if (GridView1.Rows.Count == 0)
            {
                dt = new DataTable();

                DataColumn dc1 = new DataColumn("File Name", typeof(string));

                DataColumn dc2 = new DataColumn("File Size", typeof(string));

                dt.Columns.Add(dc1);

                dt.Columns.Add(dc2);

                dr = dt.NewRow();

                dr["File Name"] = TextBox1.Text.ToString().Trim();
                if (size > 0)
                    dr["File Size"] = size.ToString() + " KB";
                else
                    dr["File Size"] = fileObj.Length.ToString() + " Bytes";


                dt.Rows.Add(dr);
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
            else
            {
                int count = GridView1.Rows.Count;

                dt = new DataTable();

                DataColumn dc1 = new DataColumn("File Name", typeof(string));

                DataColumn dc2 = new DataColumn("File Size", typeof(string));

                dt.Columns.Add(dc1);

                dt.Columns.Add(dc2);

                for (int i = 0; i < count; i++)
                {
                    dr = dt.NewRow();
                    dr["File Name"] = GridView1.Rows[i].Cells[1].Text;
                    dr["File Size"] = GridView1.Rows[i].Cells[2].Text;
                    dt.Rows.Add(dr);
                }

                dr = dt.NewRow();

                dr["File Name"] = TextBox1.Text.ToString().Trim();
                if(size > 0)
                    dr["File Size"] = size.ToString() + " KB";
                else
                    dr["File Size"] = fileObj.Length.ToString() + " Bytes";


                dt.Rows.Add(dr);

                GridView1.DataSource = dt;

                GridView1.DataBind();

            }

            TextBox1.Text = "";
        }
    }


if u wish to delete the file then just use the delete option in the Grid View.

Once all the files to be uploaded are added u can continue processing the files and Upload the files by clicking the Upload Files Button. The processing code for Upload Files Button is left to the user.


Points of Interest

This Article makes use of HTMLFile Input Control and Grid View. Its very simple and easy to work around.

Good Luck Guys.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Govardhana Reddy


Member
This is Govardhana Reddy, i am here to explore this world of INTERNET. I feel this is one way through which i can explore this world of INTERNET.

I want to be one of the best in this profession (a Software Developer, not a Software Engineer its a bit Controversial.)

My definition of a Software Engineer : "A person who knows what to cut/copy and where to paste".

Recently came up with a software firm Gyaan Solutions which provide a user-centric, web-based learning management system (LMS) for the delivery of online learning and teaching

Anyways long road ahead...

You can find more articles on http://www.apondu.50megs.com
Occupation: Founder
Company: Gyaan Solutions
Location: India India

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralWhat about Firefox Pinmemberaromr8:26 1 Oct '07  
GeneralRe: What about Firefox PinmemberEdelman11:53 1 Oct '07  
GeneralWill this approach work for Live website?? PinmemberMichael Sync16:23 30 Sep '07  
GeneralRe: Will this approach work for Live website?? PinmemberGovardhana Reddy3:28 26 Dec '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 Sep 2007
Editor:
Copyright 2007 by Govardhana Reddy
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project