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