Implementing multiple file uploads in asp.net

Source: Internet
Author: User

In previous Web applications, uploading files was a hassle, and now with. NET, file uploads have become a breeze. The following example implements the multiple file upload function. You can dynamically add input forms, and the number of uploaded files is unlimited. The code is as follows:

Multiupload.aspx

<%@ Page language="vb" autoeventwireup="false" codebehind=" multiupload.aspx.vb" inherits="aspxweb.multiupload" %> <! 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"> <center> <asp:label runat="server" Id=" Mytitle"></asp:label> < P id=" Myfile"><input type="file" size="50" name=" File"></p> < P> <input type="button" value=" Add (Add) " Onclick="addfile () "> <asp:button runat="server" text=" Upload " id=" Upload"></asp:button> <input Onclick="this.form.reset () " type="button" value=" Reset (reset) "> </P> </center> < P align="center"> <asp:label id="strstatus" runat="server" font-names=" Song Body " font-bold=" true" font-size="9pt" width="500px" borderstyle=" None&quoT bordercolor=" White"></asp:label> </P> </form> </body> </HTML>

Post code: MultiUpload.aspx.vb

Public Class multiupload Inherits System.Web.UI.Page Protected WithEvents Upload as System.Web.UI.WebControls.Button Pro tected WithEvents MyTitle as System.Web.UI.WebControls.Label Protected WithEvents as System.Web.UI.WebControls.Label #Region " Web Form Designer generated Code " ' This are required by the Web Form Designer. < System.Diagnostics.DebuggerStepThrough () > Private Sub InitializeComponent () End Sub Private Sub Page_Init (ByVal sender as System.Object, ByVal e as System.EventArgs ) Handles Mybase.init ' Codegen:this Method-Call are required by the Web Form Designer ' does not modify it using the Code EDI Tor. InitializeComponent () End Sub #End Region Private Sub Page_Load (ByVal sender as System.Object, ByVal e as System.EventArgs ) Handles MyBase.Load mytitle.text = "<h3> multiple file upload </h3>" Upload.text = " start uploading " If (Me.ispostback) Then me.saveimages () end Sub Private Function SaveimagES () as System.Boolean ' traverse file table cell element Dim files as System.Web.HttpFileCollection = System.Web.HttpContext.Current.Request.Files ' state information Dim STRMSG as New System.Text.StringBuilder (" uploaded files are: <HR color=red>") Dim IFile as System.Int32 Try for ifile = 0 to files. Count-1 ' Check file extension name Dim postedfile As System.Web.HttpPostedFile = Files (ifile) Dim fileName, FileExtension as System.strin G FileName = System.IO.Path.GetFileName (postedfile.filename) If not (fileName = String.Empty) Then fileextension = System. Io. Path.getextension (FileName) strmsg.append (" uploaded file type:" + postedFile.ContentType.ToString () + & quot;<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 Postedfile.saveas (System.Web.HttpContext.Current.Request.MapPath "images/& quot) + fileName) End If Next strstatus.text = strmsg.tostring () return True Catch Ex as System.Exception strstatus.text = Ex.message return False end Try End Function End Class

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.