Asp.net implements Multifile upload C #

Source: Internet
Author: User

C # version

UpLoad. aspx

& Lt; % @ Page language = & quot; c # & quot; Codebehind = & quot; UpLoad. aspx. cs & quot; AutoEventWireup = & quot; false & quot; Inherits = & quot; WebPortal. upload & quot; % & gt; & lt ;! Doctype html public & quot;-// W3C // dtd html 4.0 Transitional // EN & quot; & gt; & lt; HTML & gt; & lt; HEAD & gt; & lt; title & gt; Multifile upload & lt;/title & gt; & lt; script language = & quot; JavaScript & quot; & gt; function addFile () {var str = '& lt; INPUT type = & quot; file & quot; size = & quot; 50 & quot; NAME = & quot; File & quot; & gt; 'document. getElementById ('myfile '). insertAdjacentHTML (& quot; beforeEnd & quot;, str)} & lt;/script & gt; & lt;/HEAD & gt; & lt; body & gt; & lt; form id = & quot; form1 & quot; method = & quot; post & quot; runat = & quot; server & quot; enctype = & quot; multipart/form-data & quot; & gt; & lt; div align = & quot; center & quot; & gt; & lt; h3 & gt; Multifile upload & lt; /h3 & gt; & lt; P id = & quot; MyFile & quot; & gt; & lt; INPUT type = & quot; file & quot; size = & quot; 50 & quot; NAME = & quot; File & quot; & gt; & lt;/P & gt; & lt; input type = & quot; button & quot; value = & quot; Add & quot; onclick = & quot; addFile () & quot; & gt; & lt; input onclick = & quot; this. form. reset () & quot; type = & quot; button & quot; value = & quot; ReSet (reset) & quot; & gt; & lt; asp: button Runat = & quot; server & quot; Text = & quot; Start upload & quot; ID = & quot; UploadButton & quot; & gt; & lt;/asp: button & gt; & lt;/P & gt; & lt; asp: Label id = & quot; strStatus & quot; runat = & quot; server & quot; Font-Names = & quot; & quot; Font-Bold = & quot; True & quot; Font-Size = & quot; 9pt & quot; width = & quot; 500px & quot; BorderStyle = & quot; None & quot; BorderColor = & quot; White & quot; & gt; & lt;/asp: Label & gt; & lt;/P & gt; & lt;/div & gt; & lt;/form & gt; & lt;/body & gt; & lt;/HTML & gt;

UpLoad. aspx. cs

Using System; using System. collections; using System. componentModel; using System. data; using System. drawing; using System. web; using System. web. sessionState; using System. web. UI; using System. web. UI. webControls; using System. web. UI. htmlControls; namespace WebPortal {// & lt; summary & gt; // summary of UpLoad. /// Multifile Upload /// & lt;/summary & gt; public class Upload: System. web. UI. page {protected System. web. UI. webControls. button UploadButton; protected System. web. UI. webControls. label strStatus; private void Page_Load (object sender, System. eventArgs e) {// place user code here to initialize the page if (this. isPostBack) this. saveImages ();} private Boolean SaveImages () {// 'traverse the File form Element HttpFileCollection files = HttpContext. current. request. files; /// 'Status information System. text. stringBuilder strMsg = new System. text. stringBuilder (); strMsg. append (& quot; uploaded files are: & lt; hr color = red & gt; & quot;); try {for (int iFile = 0; iFile & lt; files. count; iFile ++) {// 'check the file extension name HttpPostedFile postedFile = files [iFile]; string fileName, fileExtension; fileName = System. IO. path. getFileName (postedFile. fileName); if (fileName! = & Quot;) {fileExtension = System. IO. path. getExtension (fileName); strMsg. append (& quot; type of the uploaded file: & quot; + postedFile. contentType. toString () + & quot; & lt; br & gt; & quot;); strMsg. append (& quot; client file address: & quot; + postedFile. fileName + & quot; & lt; br & gt; & quot;); strMsg. append (& quot; file name to be uploaded: & quot; + fileName + & quot; & lt; br & gt; & quot;); strMsg. append (& quot; File Upload Extension: & quot; + fileExtension + & quot; & lt; br & gt; & lt; hr & g T; & quot;); // 'can be saved to different folders according to the extension name. // Note: you may need to modify the anonymous write permission of your folder. PostedFile. saveAs (System. web. httpContext. current. request. mapPath (& quot; images/& quot;) + fileName) ;}} strStatus. text = strMsg. toString (); return true;} catch (System. exception Ex) {strStatus. text = Ex. message; return false ;}# code generated by region Web forms designer override protected void OnInit (EventArgs e) {// CODEGEN: ASP.. NET Web form designer. // InitializeComponent (); base. onInit (e) ;}/// & lt; summary & gt; // The designer supports the required methods-do not use the code editor to modify the content of this method. /// & Lt;/summary & gt; private void InitializeComponent () {this. ID = & quot; Upload & quot; this. load + = new System. eventHandler (this. page_Load) ;}# endregion }}

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.