[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 |