asp.net implementation of multiple file upload C # version

Source: Internet
Author: User
Tags file upload httpcontext

C # version

Upload.aspx

<%@ Page language="c#" codebehind=" upload.aspx.cs" autoeventwireup="false" inherits=" webportal.upload" %> <! DOCTYPE HTML public "-//W3C//DTD HTML 4.0 transitional//en" > < Html> < Head> <title> multi-File upload </title> <script language=" Javascript"> function AddFile () {var str = ' <input type="file" size="50" name=" File"> ' document.getElementById (' MyFile '). insertAdjacentHTML ("BEFOREEND",STR) } </script> </HEAD> <body> <form id="form1" method="post" runat="server" Enctype="multipart/form-data"> <div align="center"> <h3> multi-File upload </h3> < P id=" Myfile"><input type="file" size="50" name=" File"></p> < P> <input type="button" value=" Add (Add) " Onclick="addfile () "> <input Onclick="this.form.reset () " type="button" value=" Reset (reset) "> <asp:button runat="server" text=" Start uploading " id=" Uploadbutton"></asp:button> </P> < P> <asp:label id="strstatus" runat="server" font-names=" Song Body " font-bold=" true" font-size="9pt" width="500px" borderstyle=" none" bordercolor=" White"></asp:label> </P> &lT;/div> </form> </body> </HTML>

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; Summary description of namespace Webportal {///<summary>///UpLoad. Implementation of multiple file upload///</summary> 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) {///places user code here to initialize the page if (this. IsPostBack) this. Saveimages (); Private Boolean Saveimages () {///' Traverse File table cell httpfilecollection files = HttpContext.Current.Request.Files;///' state information Sy Stem. Text.stringbuilder STRMSG = new System.Text.StringBuilder (); Strmsg.append (" uploaded files are: <hr color=red>"); try {for (int ifile = 0; ifile < files. Count; ifile++) {///' check file extension name httppostedfile PostedFile = filEs[ifile]; String FileName, FileExtension; FileName = System.IO.Path.GetFileName (postedfile.filename); if (fileName!= "") {fileextension = System.IO.Path.GetExtension (fileName); Strmsg.append (" uploaded file type:" + PostedFile.ContentType.ToString () + "<br>"); Strmsg.append (" Client file address:" + postedfile.filename + "<br>"); Strmsg.append (" upload file filename:" + filename + "<br>"); Strmsg.append ( " upload file extension:" + fileextension + "<br><hr>"); ' Can be saved to a different folder depending on the extended name///Note: You may want to modify the anonymous write permission for your folder. Postedfile.saveas (System.Web.HttpContext.Current.Request.MapPath ("images/") + fileName);} Strstatus.text = Strmsg.tostring (); return true; catch (System.Exception Ex) {strstatus.text = Ex.message; return false;}} Code generated #region The Web Forms Designer override protected void OnInit (EventArgs e) {////CODEGEN: This call is required for the ASP.net Web forms Designer. InitializeComponent (); Base. OnInit (e); }///<summary> The designer supports the method you want-do not use the Code Editor to modify the contents of///this method. </summary> private void InitializeComponent () {this.id = " upload"; 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.