asp.net fileupload控制項上傳圖片並預覽圖片_實用技巧

來源:互聯網
上載者:User

本文為大家分享了fileupload控制項實現上傳圖片後並進行預覽圖片的功能,並對web.config進行了配置,先看一下最終效果:

頁面代碼:

 <form id="form1" runat="server"> <div> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="Button1" runat="server" Text="上傳" Width="54px" OnClick="Button1_Click" /> <asp:Label ID="Label1" runat="server" Text="" Style="color: Red"></asp:Label> <asp:Image runat="server" ID="Image1" Style="z-index: 102; left: 20px; position: absolute;  top: 49px" Width="73px" /> </div> </form>

後台代碼:

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;namespace Web.File{ public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } #region 檔案上傳 /// <summary> /// 檔案上傳 /// </summary> protected void Button1_Click(object sender, EventArgs e) {  if (FileUpload1.FileName == "")  {  this.Label1.Text = "上傳檔案不可為空";  return;  }  bool fileIsValid = false;  //如果確認了上傳檔案,則判斷檔案類型是否符合要求   if (this.FileUpload1.HasFile)  {  //擷取上傳檔案的尾碼   String fileExtension = System.IO.Path.GetExtension(this.FileUpload1.FileName).ToLower();  String[] restrictExtension = { ".gif", ".jpg", ".bmp", ".png" };  //判斷檔案類型是否符合要求   for (int i = 0; i < restrictExtension.Length; i++)  {   if (fileExtension == restrictExtension[i])   {   fileIsValid = true;   }   //如果檔案類型符合要求,調用SaveAs方法實現上傳,並顯示相關資訊    if (fileIsValid == true)   {   //上傳檔案是否大於10M   if (FileUpload1.PostedFile.ContentLength > (10 * 1024 * 1024))   {    this.Label1.Text = "上傳檔案過大";    return;   }   try   {    this.Image1.ImageUrl = "~/File/" + FileUpload1.FileName;    this.FileUpload1.SaveAs(Server.MapPath("~/File/") + FileUpload1.FileName);    this.Label1.Text = "檔案上傳成功!";   }   catch   {    this.Label1.Text = "檔案上傳失敗!";   }   finally   {   }   }   else   {   this.Label1.Text = "只能夠上傳尾碼為.gif,.jpg,.bmp,.png的檔案";   }  }  } } #endregion }}

Web.config 配置:

<!--因為FileUpload 控制項上傳最大為4M,如果要上傳更大檔案,改下maxRequestLength的大小--><configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> <httpRuntime requestValidationMode="2.0" maxRequestLength="10485760" executionTimeout="3600" appRequestQueueLimit="10000"/> </system.web></configuration>

為大家附3個精彩的專題:

ASP.NET控制項使用手冊

ASP.NET資料繫結控制項使用匯總

ASP.NET控制項使用匯總

親,你可以在自己的項目中實現fileupload控制項上傳圖片並進行預覽圖片的功能,這樣網站更具有實用性,基本步驟就是這些,可能還有小編遺漏的地方,希望大家諒解。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.