1、前台文件 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"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>asp.net fileupload控件上传文件_</title> </head> <body> <form runat="server"> <asp:FileUpload runat="server" /> <asp:Button runat="server" Text="Button" /> <asp:RegularExpressionValidator runat="server" ControlToValidate="FileUpload1" ErrorMessage="必须是 jpg或者gif文件" ValidationExpression="^(([a-zA-Z]:)|(\\{2}\W+)\$?)(\\(\W[\W].*))+(.jpg|.Jpg|.gif|.Gif)$"></asp:RegularExpressionValidator> </form> </body> </html>
2、后端代码 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("<img src='"https://www.jb51.net/+savePath+"'>"); } else { Page.Response.Write("fff"); } } }
去掉绿色部分就可上传任何文件,它是用一个正则表达式来验证上传文件的类型
在ASP.NET 2.0中使用FileUpload服务器控件很容易的就能将文件上传到服务器。
1、aspx文件代码
<%@ 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"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>FileUpload上传文件示例-jb51.net</title> </head> <body> <form runat="server"> <div> <asp:FileUpload runat="server" /> <asp:Button runat="server" _disibledevent="Button1_Click" Text="上传文件" /><br /> <asp:Label runat="server" Text="Label"></asp:Label></div> </form> </body> </html>
2、后端代码 aspx.cs:
protected void Button1_Click(object sender, EventArgs e) { if (FileUpload1.HasFile) { try { FileUpload1.SaveAs(Server.MapPath("upload") + "\\" + FileUpload1.FileName); Label1.Text = "客户端路径:" + FileUpload1.PostedFile.FileName + "<br>" + "文件名:" + System.IO.Path.GetFileName(FileUpload1.FileName) + "<br>" + "文件扩展名:" + System.IO.Path.GetExtension(FileUpload1.FileName) + "<br>" + "文件大小:" + FileUpload1.PostedFile.ContentLength + " KB<br>" + "文件MIME类型:" + FileUpload1.PostedFile.ContentType + "<br>" + "保存路径:" + Server.MapPath("upload") + "\\" + FileUpload1.FileName; } catch (Exception ex) { Label1.Text = "发生错误:" + ex.Message.ToString(); } } else { Label1.Text = "没有选择要上传的文件!"; } }
1、asp.net fileupload多文件上传的例子
使用fileupload实现多文件上传,可以像传单个文件那样对每个文件单独进行处理,除此之外,还可以使用HttpFileCollection类捕获从Request对象发送来的所有文件,然后再单独对每个文件进行处理。
后端代码 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 += "文件 #" + (i + 1) + ":" + System.IO.Path.GetFileName(postedFile.FileName) + "<br/>"; postedFile.SaveAs(filepath + System.IO.Path.GetFileName(postedFile.FileName)); } } catch (Exception Ex) { Label1.Text += "发生错误: " + Ex.Message; } } }
2、上传文件类型的验证
对上传文件类型的验证既可以在客户端进行,也可以在服务器端进行。
客户端可以使用验证控件来进行,这里重点介绍如何在服务器端进行验证。
以上cs文件中已用GetExtension获取了文件的扩展名,只要稍加判断即可实现上传类型验证:
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 = "客户端路径:" + FileUpload1.PostedFile.FileName + "<br>" + "文件名:" + System.IO.Path.GetFileName(FileUpload1.FileName) + "<br>" + "文件扩展名:" + System.IO.Path.GetExtension(FileUpload1.FileName) + "<br>" + "文件大小:" + FileUpload1.PostedFile.ContentLength + " KB<br>" + "文件MIME类型:" + FileUpload1.PostedFile.ContentType + "<br>" + "保存路径:" + Server.MapPath("upload") + "\\" + FileUpload1.FileName; } catch (Exception ex) { Label1.Text = "发生错误:" + ex.Message.ToString(); } } else { Label1.Text = "只允许上传rar、zip文件!"; } } else { Label1.Text = "没有选择要上传的文件!"; } }