Uploading Multiple Files Using ASP.NET
By AzamSharp
Views: 5033

Introduction:

Uploading files in ASP.NET is simple and easy as I have explained in my article Uploading Images to the Server's Folder. The thing about uploading is that you can upload one file at a time which is pretty painful if you have 100 files to upload. In this article we will see how to upload all the files in a particular folder. The technique which we are going to use here is not PERFECT but does perform the operation to some extent.

User Interface:

User Interface is pretty simple which consists of ListBox and the HTML File Field control. Take a look at the screen shot below:

So what's the catch?

Good question! ASP.NET File Field control can only be used to select files and that's what its supposed to do. So in order to get all the files of a particular folder you need to select a file from that folder. This means that if you have a folder name Exams and it has two files Test1.zip and Test2.zip than you need to select one of the zip files in order to upload all the files from the Exams folder.

What about if I don't want one of the files in the folder to be uploaded?

There are several work around to this problem.

  • You can check the extension of files you need to upload. 
  • You can only put the files to upload in that folder.
  • You can display the files in the ListBox control and later remove it depending on the user selection.

So, now you know what we are up to let's get this party started.

Here is the Upload Files Button click Code:

private void Button1_Click(object sender, System.EventArgs e)

{

ArrayList fileList = new ArrayList();

string dirNameWithPath = System.IO.Path.GetDirectoryName(File1.PostedFile.FileName);

// Get the files in the Directory

string[] files = Directory.GetFiles(dirNameWithPath);

foreach(string str in files)

{

// Add into the ArrayList

fileList.Add(System.IO.Path.GetFileName(str));

}

// Bind to the ListBox

ListBox1.DataSource = fileList;

ListBox1.DataBind();

// Run a loop and upload the files

foreach(string str in fileList)

{

File1.PostedFile.SaveAs(SERVER_FOLDER+System.IO.Path.GetFileName(str));

}

}

First we made an ArrayList so that we can bind it to the ListBox control and the user can see the uploaded files. Than we get the directory name and the path of the directory in which the files to upload belong. After that we get the files in the directory and store them in the string array. Than we ran a foreach loop and simply add all the files to the ArrayList. Than we assign the ArrayList to the ListBox control. And finally we run a foreach loop and upload all the files to the server's folder.

SERVER_FOLDER is just a constant which is the path to the server's folder.

private const string SERVER_FOLDER = @"C:\ServerFolder\";

You might see an error saying that you don't have permission to access the ServerFolder in this case just give ASPNET account permission to access the ServerFolder.

If everything goes well than you will see all the uploaded files in the folder.

I hope you liked the article, happy coding!

By AzamSharp




Enter Comment/Feedback
  •  
  •  
  •  
  •  
  •  

Comments/Feedbacks
Subject: Error
Name: Edmundo
Date: 6/5/2007 4:06:38 AM
Comment:
Hi

I was trying to implement your solution but it gives me an error:"The name 'File1' does not exist in the current context".Can you help me with that?

Thanks
Subject: something Wrong !!
Name: epoch
Date: 7/6/2007 3:45:01 AM
Comment:
the uploaded files always same. just the names are diffrent.
because of File1.PostedFile is always points same just one file...
Subject: Error
Name: Sanjeev
Date: 7/19/2007 12:04:07 AM
Comment:
Hi
Im getting the error that cannot access the Path c:\xxx

where xxx is the folder from where im selecting the file.
Subject: RE: Error
Name: AzamSharp
Date: 7/19/2007 7:59:08 AM
Comment:
Hi Sanjeev,

Make sure you have the permissions to access the folder.
Subject: Does this work?
Name: Jared Holgate
Date: 8/28/2007 1:47:34 AM
Comment:
Hi

Just had a quick look at this, as I am looking for similar functionality. Surely System.IO is looking at the server file system, rather than the client?

Thanks

Jared

Subject: Good
Name: Vikram Sharma
Date: 9/8/2007 1:40:01 PM
Comment:
This is a very simple yet great effective work done by u, while the time i was trying to search for this kind of code, i was just thinking about the same concept to do so, but luckily i came across ur code by the time.... Good Job
Subject: RE: Good
Name: AzamSharp
Date: 9/9/2007 9:29:16 AM
Comment:
Hi Vikram Sharma,

I am glad that you liked the article.
Subject: Server Path instead of Client
Name: FF
Date: 9/10/2007 8:53:37 AM
Comment:
Hi,

The code is good, but Directory.GetFiles only deals with server instead of client. If this is the case, the solution of this article is not workable. This solution can be tested successfully in local machine, but when deployed to web server, it won't work at all. Does anyone have any points about it as the idea behind this article is so practical in web application.

thanks,
Subject: This can't work
Name: Bertrand Le Roy
Date: 11/16/2007 11:21:14 AM
Comment:
Hate to say that, but as a few commenters mentioned before, this can't work. It only seems to work because you're running it locally and the client and server are the same machine, but you're making a fundamental confusion between the client and server. This can't work in a real-world setting where the client is a different machine. This unfortunately seems to confuse many readers who will just have to figure out why it works "on their machine" and not once deployed. I think you should pull that article or at least explain why it doesn't work.
Subject: RE: This can't work!
Name: AzamSharp
Date: 11/19/2007 7:16:15 AM
Comment:
Hi Bertrand Le Roy, Thanks for your comments. I agree with you that this only works since I am using it on my local machine. Update: Check out this article. http://www.koffeekoder.com/ArticleDetails.aspx?id=324
Subject: .net
Name: kavitha
Date: 3/20/2008 2:42:07 AM
Comment:
very useful





Join WebHost4Life.com







Copyright GridViewGuy 2007-2008