Asp.net uploads multiple files at a time

Source: Internet
Author: User

See a foreigner'sArticleIn Asp.net, how can we first select all the files to be uploaded and then upload them one time,
The summary is as follows:

First, add an upload file control, an "Attach" button, and a ListBox to store the file name waiting for upload,
One "Upload" button and one "DELETE button
<Form ID = "form1" method = "Post" runat = "server">
<Input id = "fileupload" style = "Z-INDEX: 101; left: 83px; width: 489px; position: absolute; top: 67px; Height: 22px"
Type = "file" size = "62" runat = "server">
<Asp: button id = "btnattach" 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: Running PX "runat =" server"
Width = "565px" Height = "93px"> </ASP: ListBox> <asp: button id = "btnupload" style = "Z-INDEX: 104; left: 91px; position: absolute; top: 198px"
Runat = "server" text = "Upload"> </ASP: button> <asp: button id = "btndelete" style = "Z-INDEX: 105; left: 684px; position: absolute; top: pixel PX"
Runat = "server" text = "delete" width = "58px"> </ASP: button>
<Asp: Label id = "label1" style = "Z-INDEX: 106; left: 166px; position: absolute; top: 199px" runat = "server"
Width = "476px" forecolor = "red"> </ASP: Label> </form>

Then, in the "Attach" buttonCodeAs follows:
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;

}
}

Obviously, viewstate is used to store the actual file name uploaded by the user.
"Attach" button to add the file to the ListBox box
Followed by the "Upload" code,
Private void btnupload_click (Object sender, system. eventargs E)
{
Int sessioncount = session. count;


/// Int sessioncount = convert. toint32 (viewstate ["I"]);
For (INT I = sessionCount-1; I> = 0; I --)
{
If (sessioncount <= 3)
{
Htmlinputfile conditioned = (htmlinputfile) session ["myupload" + I];
If (guid. postedfile. contentlength <= 500000)
{
String storepath = server. mappath ("~ ") +"/Multipleupload ";
If (! Directory. exists (storepath ))
Directory. createdirectory (storepath );
Guid. 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 ();
}

In fact, all the files to be uploaded are retrieved from the session, and each file is uploaded one by one.
Finally, the Code for deleting the file to be uploaded from The ListBox is removed from the session.
Private void btndelete_click (Object sender, system. eventargs E)
{
If (listbox1.selectedindex>-1)
{
Int uploadedfileindex = listbox1.selectedindex;
Session. Remove ("myupload" + uploadedfileindex );
Listbox1.items. Remove (listbox1.selectedvalue );
}
}

To sum up, it is better to use the session when there are not many files to upload.

Related Article

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.