Multifile upload in ASP. NET

Source: Internet
Author: User

In previous Web applications, uploading files was a very troublesome task. Now, with. NET, File Upload becomes easy. The following example implements the Multifile upload function. You can dynamically add input forms. There is no limit on the number of uploaded files. The Code is as follows:

MultiUpload. aspx

& Lt; % @ Page Language = & quot; vb & quot; AutoEventWireup = & quot; false & quot; Codebehind = & quot; MultiUpload. aspx. vb & quot; Inherits = & quot; aspxWeb. multiUpload & 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; center & gt; & lt; asp: Label Runat = & quot; server & quot; ID = & quot; myTitle & quot; & gt; & lt;/asp: Label & 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; P & gt; & lt; input type = & quot; button & quot; value = & quot; Add (Add) & quot; onclick = & quot; addFile () & quot; & gt; & lt; asp: Button Runat = & quot; server & quot; Text = & quot; Upload & quot; ID = & quot; Upload & quot; & gt; & lt;/asp: Button & gt; & lt; input onclick = & quot; this. form. reset () & quot; type = & quot; button & quot; value = & quot; ReSet (reset) & quot; & gt; & lt;/P & gt; & lt;/center & gt; & lt; P align = & quot; center & quot; & 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;/form & gt; & lt;/body & gt; & lt;/HTML & gt;

Code: MultiUpload. aspx. vb

Public Class MultiUploadInherits System. web. UI. pageProtected WithEvents Upload As System. web. UI. webControls. buttonProtected WithEvents MyTitle As System. web. UI. webControls. labelProtected WithEvents strStatus As System. web. UI. webControls. label # Region & quot; Web Form Designer Generated Code & quot; 'This call is required by the Web Form Designer. & lt; System. diagnostics. debuggerStepThrough () & gt; Private Sub InitializeComponent () End SubPrivate Sub Page_Init (ByVal sender As System. object, ByVal e As System. eventArgs) Handles MyBase. init 'codegen: This method call is required by the Web Form Designer 'Do not modify it using the code editor. initializeComponent () End Sub # End RegionPrivate Sub Page_Load (ByVal sender As System. object, ByVal e As System. eventArgs) Handles MyBase. loadMyTitle. text = & quot; & lt; h3 & gt; Multifile Upload & lt;/h3 & gt; & quot; Upload. text = & quot; Start upload & quot; If (Me. isPostBack) Then Me. saveImages () End SubPrivate Function SaveImages () As System. boolean 'traverses the File form Element Dim files As System. web. httpFileCollection = System. web. httpContext. current. request. files 'status information Dim strMsg As New System. text. stringBuilder (& quot; uploaded files are: & lt; hr color = red & gt; & quot;) Dim iFile As System. int32TryFor iFile = 0 To files. count-1 'check the file extension name Dim postedFile As System. web. httpPostedFile = files (iFile) Dim fileName, fileExtension As System. stringfileName = System. IO. path. getFileName (postedFile. fileName) If Not (fileName = String. empty) ThenfileExtension = 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 & gt; & quot ;) 'different extended names can be saved to different folders postedFile. saveAs (System. web. httpContext. current. request. mapPath (& quot; images/& quot;) + fileName) End IfNextstrStatus. text = strMsg. toString () Return TrueCatch Ex As System. exceptionstrStatus. text = Ex. messageReturn FalseEnd TryEnd FunctionEnd 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.