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