ASP. NET FileUpload application instance

Source: Internet
Author: User

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">

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Asp: FileUpload ID = "FileUpload1" runat = "server"/>
<Asp: Button ID = "Button1" runat = "server" OnClick = "button#click" Text = "Button"/>
<Asp: RegularExpressionValidator ID = "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) {$ article $} quot;> </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: \ 111 \";
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 (" ");

}
Else
{
Page. Response. Write ("fff ");
}
}

}

Remove the green part to upload any file. It uses a regular expression to verify the type of the file to be uploaded.

 

Example 2:

 

Using the FileUpload Server Control in ASP. NET 2.0 can easily upload files to the server. A simple example is as follows:

Aspx:

Program code
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "fileupload. aspx. cs" Inherits = "fileupload" %>

<〈! 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> FileUpload File Upload example -Mzwu.com </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Asp: FileUpload ID = "FileUpload1" runat = "server"/>
<Asp: Button ID = "Button1" runat = "server" _ disibledevent = "button#click" Text = "Upload File"/> <br/>
<Asp: Label ID = "Label1" runat = "server" Height = "269px" Text = "Label" Width = "360px"> </asp: Label> </div>
</Form>
</Body>
</Html>

Aspx. cs:

Program code
Protected void button#click (object sender, EventArgs e)
{
If (FileUpload1.HasFile)
{
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 = "no file to upload is selected! ";
}

}

1. upload multiple files at a time

To upload multiple files at a time, we can process each file as a leaflet file. In addition, we can also use the HttpFileCollection class to capture all the files sent from the Request object, then process each file separately. The Code is as follows:

Aspx. cs:

Program code
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;
}
}
}

2. Verify the Upload File Type

You can verify the file type on either the client or the server. The client can use the verification control, but today we mainly talk about how to verify on the server. In the above cs file, the file extension has been obtained using GetExtension. You only need to make a slight judgment to verify the upload type:

Aspx. cs:

Program code
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! ";
}

}

It should be noted that we should not rely too much on the client-side verification control and the server-side verification method, because users only need to change the file extension to the allowed type to avoid the above verification, this is not difficult for users.

3. Solve the file size limit

In ASP. NET 2.0, the maximum size of the file to be uploaded is 4 MB by default. However, you can modify the default value in web. cofig. The related nodes are as follows:

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.

4. "multipart/form-data" and Request coexist

In ASP, once a file is uploaded using a form (the value of form's enctype is multipart/form-data), the server cannot use Request. form to obtain the value of the Form. NET 2.0 does not exist anymore:

Aspx. cs:

Program code
Protected void button#click (object sender, EventArgs e)
{
If (FileUpload1.HasFile)
{
Try
{
FileUpload1.SaveAs (Server. MapPath ("upload") + "\" + FileUpload1.FileName );
Label1.Text = "Upload File:" + FileUpload1.FileName + "<br>" +
"NOTE:" + Request. Form ["TextBox1"]; // you can also use "TextBox1.Text" to obtain the description.
}
Catch (Exception ex)
{
Label1.Text = "error:" + ex. Message. ToString ();
}
}
Else
{
Label1.Text = "no file to upload is selected! ";
}
}

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.