ASP. NET multi-File Upload

Source: Internet
Author: User

I have compiled a multi-file upload function and shared it with you. For more information, see.
Page:
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" validaterequest = "false" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> untitled page </title>
<SCRIPT type = "text/JavaScript">
// Add a client nearby
Function AddFile (){
VaR STR = '<input type = "file" name = "file"/> <input type = "button" value = "Remove" onclick = "remove (this) "/> <br/> ';
Document. All. Files. insertadjacenthtml ("beforeend", STR );
}
// Near the client Deletion
Function remove (OBJ ){
OBJ. style. Display = "NONE ";
OBJ. nextsibling. style. Display = "NONE ";
OBJ. previussibling. style. Display = "NONE ";
OBJ. previussibling. Disabled = "disabled ";
}

</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>

<Div id = "Files">
<% -- There must be an Asp.net server fileupload control -- %>
Multifile upload <br/>
<Asp: fileupload id = "fileupload1" runat = "server"/> <input type = "button" value = "Remove" onclick = "remove (this) "/> <br/>
</Div>
<Input id = "button1" type = "button" value = "add" onclick = "AddFile ()"/>
<Input id = "button2" type = "button" value = "reset" onclick = "This. Form. Reset ()"/>
<Asp: button id = "button3" runat = "server" onclick = "button3_click" text = "Upload" causesvalidation = "false"/> </div>
</Form>
</Body>
</Html>
CodePart:

Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. IO;
Using system. net;
Using system. text;

Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{

}
Protected void button3_click (Object sender, eventargs E)
{
Httpfilecollection filelist = httpcontext. Current. Request. files;
// Httpfilecollection filelist = request. files;
Stringbuilder strb = new stringbuilder ();
For (INT I = 0; I <filelist. Count; I ++)
{
Httppostedfile file = filelist [I];
String filename = path. getfilename (file. filename );
String fileextension = path. getextension (filename );

If (file. contentlength> 0)
{
File. saveas (server. mappath ("upload/" + filename ));
Strb. append (string. Format ("file {0} uploaded successfully, extension: {1} \ n", filename, fileextension ));

}

}
// Prompts the upload status
Clientscript. registerclientscriptblock (typeof (PAGE), "", "alert ('" + strb. tostring () + "')", true );
}
}

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.