ASP.NET實例教程之文件上傳
- 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>無標題頁</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <asp:FileUpload ID="FileUpload1" runat="server" />
- <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
- <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="FileUpload1"
- ErrorMessage="必須是 jpg或者gif文件" ValidationExpression="^(([a-zA-Z]:)|(\\{2}\W+)\$?)(\\(\W[\W].*))+(.jpg|.Jpg|.gif|.Gif)$"></asp:RegularExpressionValidator>
- </form>
- </body>
- </html>
- 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='"+savePath+"'>");
- }
- else
- {
- Page.Response.Write("fff");
- }
- }
- }
去掉綠色部分就可上傳任何文件,它是用一個正則表達式來驗證上傳文件的類型
例子二:
在ASP.NET 2.0中使用FileUpload服務器控件很容易的就能將文件上傳到服務器,一個簡單的例子如下:
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上傳文件示例-Mzwu.com〈/title>
- 〈/head>
- 〈body>
- 〈form id="form1" runat="server">
- 〈div>
- 〈asp:FileUpload ID="FileUpload1" runat="server" />
- 〈asp:Button ID="Button1" runat="server" _disibledevent="Button1_Click" Text="上傳文件" />〈br />
- 〈asp:Label ID="Label1" runat="server" Height="269px" Text="Label" Width="360px">〈/asp:Label>〈/div>
- 〈/form>
- 〈/body>
- 〈/html>
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 = "沒有選擇要上傳的文件!";
- }
- }
ASP.NET實例教程1.一次上傳多個文件
要一次上傳多個文件,我們可以像傳單個文件那樣對每個文件單獨進行處理,除此之外,我們還可以使用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;
- }
- }
- }
ASP.NET實例教程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 = "沒有選擇要上傳的文件!";
- }
- }
需要注意的是,我們不能過分依賴于客戶端驗證控件和服務器端上述方法的驗證,因為用戶只需將文件擴展名更改為允許的類型就可以避開上邊的驗證,這對用戶來說并不是件困難的事情。
ASP.NET實例教程3.解決文件大小限制
在ASP.NET 2.0中FileUpload默認上傳文件最大為4M,不過我們可以在web.cofig中修改相關節點來更改這個默認值,相關節點如下:
程序代碼
- 〈system.web>
- 〈httpRuntime maxRequestLength="40690" executionTimeout="6000" />
- 〈/system.web>
maxRequestLength表示可上傳文件的最大值,executionTimeout表示ASP.NET關閉前允許發生的上載秒數。
ASP.NET實例教程4."multipart/form-data"和Request共存
在ASP程序中一旦使用表單上傳文件(form的enctype屬性值為multipart/form-data),服務器端就不能再用Request.Form來獲取表單的值,這種限制在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 = "上傳文件:" + FileUpload1.FileName + "〈br>" +
- "說明:" + Request.Form["TextBox1"];//也可以用"TextBox1.Text"來獲取說明
- }
- catch (Exception ex)
- {
- Label1.Text = "發生錯誤:" + ex.Message.ToString();
- }
- }
- else
- {
- Label1.Text = "沒有選擇要上傳的文件!";
- }
- }
【編輯推薦】