ASP. NET File Upload

Source: Internet
Author: User

Example 1:

Use a regular expression to verify the type of the file to be uploaded. Remove <asp: RegularExpressionValidator/> to upload all

 

Default. aspx:

<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "_ Default" %> 

 

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

 

<HtmlXmlns = "http://www.w3.org/1999/xhtml"> 

<HeadRunat = "server"> 

<Title>No title page</Title> 

</Head> 

<Body> 

<FormId = "form1" runat = "server"> 

<Asp: FileUploadID = "FileUpload1" runat = "server"/> 

<Asp: ButtonID = "Button1" runat = "server" OnClick = "button#click" Text = "Button"/> 

<Asp: RegularExpressionValidatorID = "RegularExpressionValidator1" runat = "server" ControlToValidate = "FileUpload1"

ErrorMessage = "must be a jpg or gif file" ValidationExpression = "^ ([a-zA-Z] :) | (\ {2} \ W +) \ $ ?) (\ W [\ W]. * ))((.jpg |. Jpg |. gif |. Gif) $"> </Asp: RegularExpressionValidator> 

</Form> 

 

</Body> 

</Html> 

 

 

Default. aspx. cs:

 

Using System;

Using System. Data;

Using System. Configuration;

Using System. Web;

Using System. Web. Security;

Using System. Web. UI;

Using System. Web. UI. WebControls;

Using System. Web. UI. WebControls. WebParts;

Using System. Web. UI. HtmlControls;

 

Public partial class _ Default: System. Web. UI. Page

{

Protected void Page_Load (object sender, EventArgs e)

{

 

}

Protected void button#click (object sender, EventArgs e)

{

String savePath = @ "F: \ Upload \";

If (FileUpload1.HasFile)

{

String filename;

Filename = FileUpload1.FileName;

SavePath + = filename;

FileUpload1.SaveAs (savePath );

Page. Response. Write (FileUpload1.PostedFile. ContentType + FileUpload1.PostedFile. ContentLength +"<Br>");

Page. Response. Write ("Src = '"+ savePath + "'>");

 

}

Else

{

Page. Response. Write ("fff ");

}

}

}

 

 

 

Example 2:

Obtain and verify the attributes of the uploaded file.

Protected void button#click (object sender, EventArgs e)

{

If (FileUpload1.HasFile)

{

FileExt = System. IO. Path. GetExtension (FileUpload1.FileName );

If (fileExt = ". rar" | fileExt = ". zip ")

{

Try

{

FileUpload1.SaveAs (Server. MapPath ("upload") + "\" + FileUpload1.FileName );

Label1.Text = "client path:" + FileUpload1.PostedFile. FileName + "<br>"+

"File name:" + System. IO. Path. GetFileName (FileUpload1.FileName) + "<br>"+

"File Extension:" + System. IO. Path. GetExtension (FileUpload1.FileName) + "<br>"+

"File size:" + FileUpload1.PostedFile. ContentLength + "KB <br>"+

"File MIME type:" + FileUpload1.PostedFile. ContentType + "<br>"+

"Save path:" + Server. MapPath ("upload") + "\" + FileUpload1.FileName;

}

Catch (Exception ex)

{

Label1.Text = "error:" + ex. Message. ToString ();

}

}

Else

{

Label1.Text = "only rar and zip files can be uploaded! ";

}

}

Else

{

Label1.Text = "no file to upload is selected! ";

}

}

Example 3:

Upload multiple files at a time, use the HttpFileCollection class to capture all files sent from the Request object, and then process each file separately.

Protected void button#click (object sender, EventArgs e)

{

String filepath = Server. MapPath ("upload") + "\\";

HttpFileCollection uploadFiles = Request. Files;

For (int I = 0; I <uploadFiles. Count; I ++)

{

HttpPostedFile postedFile = uploadFiles [I];

Try

{

If (postedFile. ContentLength>0)

{

Label1.Text + = "file #" + (I + 1) + ":" + System. IO. Path. GetFileName (postedFile. FileName) + "<br/>";

PostedFile. SaveAs (filepath + System. IO. Path. GetFileName (postedFile. FileName ));

}

}

Catch (Exception Ex)

{

Label1.Text + = "error:" + Ex. Message;

}

}

}

 

 

Example 4: Solve the size limit of uploaded files

In ASP. NET 2.0, the maximum size of the file to be uploaded by FileUpload is 4 MB by default. However, you can modify the default value in the web. cofig file.

Program code

<System. web> 

<HttpRuntime maxRequestLength = "40690" executionTimeout = "6000"/> 

</System. web> 

MaxRequestLength indicates the maximum value of a file to be uploaded, and executionTimeout indicates the number of uploads allowed before ASP. NET is disabled.

 

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.