Asp.net fileupload control uploads files and multiple files, asp. netfileupload

Source: Internet
Author: User

Asp.net fileupload control uploads files and multiple files, asp. netfileupload

1. Foreground file 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"> 

2. backend code 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 Button1_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("

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

Using the FileUpload Server Control in ASP. NET 2.0 can easily upload files to the server.

1. aspx File 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"> 

2. backend code aspx. cs:

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. Example of asp.net fileupload Multifile upload

Using fileupload to upload multiple files, you can process each file as a leaflet file. In addition, you can use the HttpFileCollection class to capture all files sent from the Request object, then process each file separately.

Backend code aspx. cs:

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. Here we will focus on how to verify it on the server.

The above cs file has used GetExtension to get the file extension. You only need to make a slight judgment to verify the upload type:
Aspx. cs:

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

Note: You cannot rely too much on the verification of the client verification control and the above method on the server, because you 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 by FileUpload is 4 MB by default. However, you can modify the default value in the web. cofig file. The related nodes are as follows:

Copy codeThe Code is as follows:
<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. Coexistence of "multipart/form-data" and Request

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:

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>" + "Description:" + 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.