几种文件上传的方法 第一种(务器控件): 1.Default.aspx代码如下: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>文件上传</title> </head> <body> <form id="form1" action="Default2.aspx" enctype="multipart/form-data" method="post"> <div> <input name="oneFile" type="file"/> <input name="btton" type="submit" value="上传" /> </div> </form> </body> </html> 2.Default.aspx.cs代码如下: using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { HttpFileCollection files = HttpContext.Current.Request.Files; HttpPostedFile file = files["oneFile"]; string FileName = System.IO.Path.GetFileName(file.FileName); string serverpath = "D:/Admin/Image/" + DateTime.Now.ToString("yyMMddhhmmss") + FileName; string strExping = FileName.Substring(FileName.LastIndexOf(".")); if (strExping.Equals(".jpg")) { file.SaveAs(serverpath); Response.Write("成功"); } else { Response.Write("失败"); } } } } 第二种(非服务器控件): 1.Default.aspx代码如下: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>未命名頁面</title> </head> <body> <form id="form1" action="Default2.aspx" enctype="multipart/form-data" method="post"> <div> <input name="oneFile" type="file"/> <input name="btton" type="submit" value="上传" /> </div> </form> </body> </html> 2.Default.aspx.cs代码如下: public void FileUpload() { HttpFileCollection files = HttpContext.Current.Request.Files; HttpPostedFile postedFile = files["oneFile"]; if (postedFile.ContentLength > 3072000) Response.Write("文件大小不能大于300K!"); string fileName, fileExtension; fileName = System.IO.Path.GetFileName(postedFile.FileName); string SaveFilePath = ""; if (fileName != "") { //SaveFilePath = "/UploadFile/" + System.DateTime.Now.ToString("yyyymmddhhmmss") + ".rar"; SaveFilePath = System.DateTime.Now.ToString("yyyymmddhhmmss") + ".rar"; fileExtension = System.IO.Path.GetExtension(fileName).ToLower(); if (fileExtension != ".rar") Response.Write("文件格式不正确,你只能上传WinRar压缩包格式文件!"); postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("UploadFile/") + SaveFilePath); //postedFile.SaveAs(Server.MapPath(SaveFilePath)); } } 第三种(服务器控件): (Default.aspx代码如下:跟第一种第二种的都差不多,就不写了) 1.Default.aspx.cs代码如下: protected void Button1_Click(object sender, EventArgs e) { if (FileUpload1.HasFile) { FileUpload1.SaveAs (Server.MapPath ( "~/Upload/" +FileUpload1.FileName)); Label1.Text="恭喜你,上传成功!"; } else { Label1.Text = "上传失败,或没指定正确的文件!"; } } 第四种(服务器控件控制上传文件大小,类型 ): 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 id="Head1" runat="server"> <title>无标题页</title> </head> <body> <form id="form1" runat="server"> <div> <asp:FileUpload ID="FileUpload1" runat="server" Width="325px" /> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传" Width="44px" /><br /> 上传文件名:<asp:Label ID="Label2" runat="server"></asp:Label><br /> 上传文件大小:<asp:Label ID="Label3" runat="server"></asp:Label><br /> 上传文件路径及名:<asp:Label ID="Label4" runat="server"></asp:Label><br /> <asp:Label ID="Label1" runat="server" ForeColor="Red"></asp:Label><br /> </div> </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) { if (FileUpload1.HasFile) { Label2.Text = FileUpload1.FileName; Label3.Text = FileUpload1.PostedFile.ContentLength.ToString() + "字节"; Label4.Text = FileUpload1.PostedFile.FileName; //获得扩展名 string extension = System.IO.Path.GetExtension(FileUpload1.FileName); if (extension == ".rar" || extension == ".doc" || extension == ".xls") { if (FileUpload1.PostedFile.ContentLength < 10000000)//文件小于10M { FileUpload1.SaveAs(Server.MapPath("~/upLoad/" + FileUpload1.FileName)); Label1.Text = "恭喜你,上传成功!"; } else { Label1.Text = "上传失败,不是管理员,上传文件不能大于10M!"; } } else Label1.Text = "上传失败,只能上传扩展名是rar、doc、xls的文件!"; } else { Label1.Text = "上传失败,或没指定正确的文件!"; } } }
4:多个文件一起上传的方法: