Upload multiple files at a time

Source: Internet
Author: User

Upload multiple files at a time
This document describes how to upload multiple files at a time.

Introduction
[Back to Top]

I'm sure that you have seen a lot of examples about how to upload multiple files using multiple instances of the htmlinputfile (Asp. /NET 1.1) or the fileupload control (Asp. NET 2.0 ). what I tried to do in this article is to provide a way to upload multiple files using one instance of the specified control. each time the user chooses a file, it will get added to a list box control where he can also delete a certain file after it has been selected. for each selected file (once being added to the list box control), I'm saving its information in a session variable and once uploaded has succeeded, the session variables are disposed. taking into considerations that saving a lot of objects into session variables will increase the load on the server, I restrict the maximum number of uploaded files to 3, and do some checking on the file content to ensure it does not exceed 5 MB. these numbers can all be modified to fit your needs when you implement this.

Starting to code
[Back to Top]

The controls being used are

1.One htmlinputfile Control

2.A button to add the file to the list box

3.A list box control to show the user his selected files

4.A Delete button to remove a previusly selected file

5.A button to save the files into the specific directory

The below listing will show how the HTML code will look after we have added the controls into our webform.

Listing 1

 <  Input  Style = "Z-INDEX: 101; left: 83px; width: 489px; position: absolute; top: 67px; Height: 22px"  Type  = "File"              Runat  = "Server"  ID  = "Fileupload"  Size  = "62"  >              <  ASP: button  ID  = "Button1"              Style  = "Z-INDEX: 102; left: 591px; position: absolute; top: 66px"              Runat  = "Server"  Text = "Attach"  >  </  ASP: button  >              <  ASP: ListBox  ID  = "Listbox1"              Style  = "Z-INDEX: 103; left: 84px; position: absolute; top: pixel"              Runat  = "Server"  Height  = "93px"   Width  = "565px"  >  </  ASP: ListBox  >              <  ASP: button  ID  = "Button2"             Style  = "Z-INDEX: 104; left: 339px; position: absolute; top: 205px"              Runat  = "Server"  Text  = "Upload"  >  </  ASP: button  >              <  ASP: button  ID  = "Button3"              Style  = "Z-INDEX: 105; left: 684px; position: absolute; top: pixel"              Runat  = "Server"  Text  = "Delete"   Width  = "70px" >  </  ASP: button  > 

Figure 1

 
 

 

Code behind (C #)

In this section, we will see how each button click event is handled to provide the correct behavior.

Listing 2

 Private  Void Btnattach_click ( Object Sender, system. eventargs e ){ // Save the attached file to filename variable  String Filename = Fileupload. postedfile. filename; // If the counter is null then create one with default value equal to 0  If (Viewstate [ "I" ] = Null ) {Viewstate [ "I" ] = 0 ;} // Check if a file is selected  If (Filename! =  Null | Filename!=   String . Empty ){ // Add it to the collection Listbox1.items. Add (fileupload. postedfile. filename ); // Save an index for each selected file  Int I = Convert. toint32 (viewstate [ "I" ]); // Save the fileupload control into a different session Session [ "Myupload"  + I] = Fileupload; // Increment the counter I ++; // Set the viewsate to the latest counter value. Viewstate [ "I" ] = I ;}}

In the above listing, we are handling the attach button click event. first, we will get the selected file name. we used the viewstate to save the counter (How many files selected ). if a file has been selected, we will save its filename as a new item in the ListBox control, then save its instance in the session state. in the final two lines of code, we are updating the counter value and resaving it in viewstate.

Figure 2

 

The figure above is showing the process that we just explained. Each selected item is being added to the ListBox control.

Listing 3

Private VoidBtndelete_click (ObjectSender, system. eventargs e ){If(Listbox1.selectedindex>-1 ){IntUploadedfileindex=Listbox1.selectedindex; Session. Remove ("Myupload" +Uploadedfileindex); listbox1.items. Remove (listbox1.selectedvalue );}}

In the above listing, we are handling the delete button click event. first we are checking if an item in the ListBox control is being selected. we are saving the selected index into a variable, setting its specific information in the session state to null, and finally it gets removed from The ListBox items collection.

Figure 3

The above figure is showing the ListBox control after we have selected the second item and pressed the delete button.

Listing 4

 Private  Void Btnupload_click ( Object Sender, system. eventargs e ){ Int Sessioncount = Session. CountFor ( Int I = Sessioncount - 1; I> = 0; I --){ If (Sessioncount <= 3) {htmlinputfile = (Htmlinputfile) session [ "Myupload"  + I]; If (Guid. postedfile. contentlength <= 500000 ){ String Storepath = Server. mappath ( "~ " ) +  "/Multipleupload" ; If (! Directory. exists (storepath) directory. createdirectory (storepath); conditioned. postedfile. saveas (storepath +   "/"  + Path. getfilename (guid. postedfile. filename); label1.text =  "Your files are uploaded successfully" ; Listbox1.items. Clear ();} Else Label1.text =  "An error occured" ;} Else Label1.text =  "You have exceeded the maximum number of files to be uploaded (3 )" ;} Session. removeall ();}

The last stage in our code behind is the upload click event. as you can see, the first thing to do is to get the number of the session variables. because all the selected files are being saved inside session variables, We will loop through all of the sessions to upload its specified file. note that we are creating a new folder called multipleupload where all the files are being saved on the Web server. do not forget to give the write permission for ASPnet user to avoid the access is denied error. after the files have been uploaded, a message will be displayed to inform the user about the upload status. finally we clear all the items in the ListBox and the session variables.

Figure 4

The above figure is showing the final stage when the files have been successfully uploaded.

Remember to modify the

Source codeDownload:

Upload/2007_04/07040908415915.zip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.