1, the front desk document 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.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 section to upload any file, it is a regular expression to verify the type of uploaded files
Using FileUpload server controls 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> "+
" FileName: "+ System.IO.Path.GetFileName ( Fileupload1.filename) + "<br>" +
"file name 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 = "Did not select the file to upload!" ";
}
}
1, asp.net fileupload multiple file Upload example
With FileUpload for multiple file uploads, you can handle each file separately as a flyer file, and you can use the Httpfilecollection class to capture all the files sent from the request object and then process each file separately.
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.contentlength > 0)
{
Label1.Text + = "File #" + (i + 1) + ":" + System.IO.Path.GetF Ilename (postedfile.filename) + "<br/>";
Postedfile.saveas (filepath + System.IO.Path.GetFileName (postedfile.filename));
}
catch (Exception Ex)
{
Label1.Text + = "Error occurred:" + ex.message
;
}
}}
2, upload file types of verification
Verification of the upload file type can be done either on the client or on the server side.
The client can use a validation control, which focuses 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 {Fileup Load1.
SaveAs (Server.MapPath ("upload") + "\" + fileupload1.filename); Label1.Text = "Client path:" + FileUpload1.PostedFile.FileName + <br> "+" FileName: "+ System.IO.Path.GetFileName ( Fileupload1.filename) + "<br>" + "file name 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; The catch (Exception ex) {Label1.Text = "error occurred:" + ex.
Message.tostring (); } else {Label1.Text = "only allowed to upload rar, zip file!"
";
}
} else {Label1.Text = "did not select the file to upload!"
"; }
}
Note that you cannot rely too much on the validation of client-side validation controls and the above methods on the server, because users can avoid the above validation by simply changing the file name extension to a permitted type, which is not a difficult task for users.
3. resolve File Size limit
In asp.net 2.0 fileupload the default upload file is up to 4M, but you can modify the associated node in Web.cofig to change this default value, the relevant node is as follows:
Copy Code code as follows:
<system.web>
</system.web>
maxRequestLength represents the maximum number of files that can be uploaded, executiontimeout indicates how many seconds are allowed to occur before the ASP.net is closed.
4, "Multipart/form-data" and request coexistence
Once you use the form upload file (the form's Enctype property value is Multipart/form-data) in an ASP program, the server side cannot use Request.Form to get the value of the form, which is no longer available in asp.net 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 be used "TextBox1.Text" To get the description} catch (Exception ex) {Label1.Text = "error occurred:" + ex.
Message.tostring (); } else {Label1.Text = "did not select the file to upload!"
"; }
}