ASP. NET fileupload control uploading files and uploading multiple files

Source: Internet
Author: User
1, front desk 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. Back-end 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.has File) {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 section to upload any file, it uses a regular expression to verify the type of the uploaded file

Using the FileUpload server control in ASP. NET 2.0 makes it easy to 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. Back-end Code Aspx.cs:

protected void Button1_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 occurred:" + ex. Message.tostring ();} } else {Label1.Text = "No files were selected for uploading! "; }}

1, ASP. FileUpload Multi-File Upload example

With FileUpload, you can use the Httpfilecollection class to capture all of the files sent from the Request object, and then process each file separately, using a multi-file upload that allows you to process each file individually like a flyer file.

Back-end Code Aspx.cs:

protected void Button1_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.contentlengt H > 0) {label1.text + = "File #" + (i + 1) + ":" + System.IO.Path.GetFileName (postedfile.filename) + "<br/>"; posted File.saveas (filepath + System.IO.Path.GetFileName (postedfile.filename));}} catch (Exception Ex) {Label1.Text + = "Error occurred:" + Ex.message;}}}

2. Upload file Type verification
Validation of uploaded file types can be performed either on the client or on the server side.
The client can use validation controls, which focus on how to authenticate on the server side.

The above CS file has been used getextension to obtain the file extension, as long as a little judgment can be implemented upload type verification:
Aspx.cs:

protected void Button1_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 occurred:" + ex. Message.tostring ();}} Else{label1.text = "Only allow uploading rar, ZIP files!" ";} } else {Label1.Text = "No files were selected for uploading! "; }}


Note that it is not overly dependent on the validation of the client-side validation controls and the methods described above by the server, because it is not difficult for users to simply change the file name extension to the allowed type to avoid the above validation.

3. fix file size limit
In ASP. FileUpload The default upload file is up to 4M, but you can change the default value in Web.cofig by modifying the associated node as follows:

<system.web> 

maxRequestLength represents the maximum number of files that can be uploaded, executiontimeout represents the upload seconds allowed before ASP.

4. Coexistence of "multipart/form-data" and request

Once you upload a file using a form in an ASP program (the Enctype property value of the form is Multipart/form-data), the server side can no longer use Request.Form to get the value of the form, which no longer exists in ASP. 2.0:

Aspx.cs:

protected void Button1_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"];//can also use "TextBox1.Text" To get instructions}catch (Exception ex) {Label1.Text = "error occurred:" + ex. Message.tostring ();} } else {Label1.Text = "No files were selected for uploading! "; }}


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.