ASP. NET multi-File Upload instance

Source: Internet
Author: User

In Web application development, you cannot use the file upload function. However, it was a very troublesome task to upload files before. NET now makes File Upload 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:

<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "MultiFileUpload. aspx. cs"

Inherits = "MultiFileUpload" %>

 

<! 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> ASP. NET multi-file upload test </title>

 

<Script type = "text/javascript">

Function addFile (){

Var div = document. createElement ("div ");

Var f = document. createElement ("input ");

F. setAttribute ("type", "file ")

F. setAttribute ("name", "File ")

F. setAttribute ("size", "50 ")

Div. appendChild (f)

Var d = document. createElement ("input ");

D. setAttribute ("type", "button ")

D. setAttribute ("onclick", "deteFile (this )");

D. setAttribute ("value", "Remove ")

Div. appendChild (d)

Document. getElementById ("_ container"). appendChild (div );

}

 

Function deteFile (o ){

While (o. tagName! = "DIV") o = o. parentNode;

O. parentNode. removeChild (o );

}

</Script>

 

</Head>

<Body>

<Form id = "form1" runat = "server" method = "post" enctype = "multipart/form-data">

<H3> Multifile upload

Username: <asp: TextBox ID = "TextBox1" runat = "server"> </asp: TextBox>

<Div id = "_ container">

<Input type = "file" size = "50" name = "File"/>

</Div>

<Div>

<Input type = "button" value = "Add file" onclick = "addFile ()"/>

</Div>

<Div <asp: Button runat = "server" Text = "Start upload" ID = "UploadButton"

Onclick = "UploadButton_Click"> </asp: Button>

</Div>

<Div>

<Asp: Label ID = "strStatus" runat = "server" Font-Names = "" Font-Bold = "True" Font-Size = "9pt"

Width = "500px" BorderStyle = "None" BorderColor = "White"> </asp: Label>

</Div>

</Form>

</Body>

</Html>

 

Using System;

Using System. Collections. Generic;

Using System. Linq;

Using System. Web;

Using System. Web. UI;

Using System. Web. UI. WebControls;

 

Public partial class MultiFileUpload: System. Web. UI. Page

{

Protected void UploadButton_Click (object sender, EventArgs e)

{

/// 'Traverse the File form Element

HttpFileCollection files = HttpContext. Current. Request. Files;

 

/// 'Status Information

System. Text. StringBuilder strMsg = new System. Text. StringBuilder ("your username is:" + TextBox1.Text + "<br/> ");

StrMsg. Append ("the uploaded files are:

Try

{

For (int iFile = 0; iFile <files. Count; iFile ++)

{

/// 'Check the extension name of the file

HttpPostedFile postedFile = files [iFile];

String fileName, fileExtension;

FileName = System. IO. Path. GetFileName (postedFile. FileName );

If (fileName! = "")

{

FileExtension = System. IO. Path. GetExtension (fileName );

StrMsg. Append ("type of the uploaded file:" + postedFile. ContentType. ToString () + "<br> ");

StrMsg. Append ("client file address:" + postedFile. FileName + "<br> ");

StrMsg. Append ("file name to be uploaded:" + fileName + "<br> ");

StrMsg. Append ("File Upload Extension:" + fileExtension + "<br>

/// 'Can be saved to different folders based on different extension names

/// Note: you may need to modify the anonymous write permission of your folder.

PostedFile. SaveAs (System. Web. HttpContext. Current. Request. MapPath ("images/") + fileName );

}

}

StrStatus. Text = strMsg. ToString ();

}

Catch (System. Exception Ex)

{

StrStatus. Text = Ex. Message;

}

 

}

}

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.