Click here to Skip to main content
Email Password   helpLost your password?
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.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralWhat about Firefox
aromr
8:26 1 Oct '07  
I did not try this code, but I know for sure that .click() method is not implemented in Firefox .

did you try it on fire fox?

____________________________
There are things worth the wait !!
Etisalat
www.etisalat.com.eg

GeneralRe: What about Firefox
Edelman
11:53 1 Oct '07  
i think that instead of .click() you can use .onmouseup() or something equivalent.


GeneralWill this approach work for Live website??
Michael Sync
16:23 30 Sep '07  
I think your approach is pretty strange for me. You are just copying the files from local drive to server so i think it will work for local intranet but I'm not sure whether it is gonna work for live website.



Thanks and Regards,
Michael Sync ( Blog: http://michaelsync.net)


GeneralRe: Will this approach work for Live website??
Govardhana Reddy
3:28 26 Dec '07  
Hi..

U got it right.. i am jst copying the files from local drives to the web server and it would work not only in intra net but also in internet.. if u go through the code u can see that i am using the address of the server.

U can try it out..

Good luck

Regards,
Govardhana Reddy R
apondu@gmail.com
http://www.apondu.50megs.com


Last Updated 28 Sep 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010