如何上傳圖片,並且限制上傳圖片文件類型

 

  1. <html xmlns="http://www.w3.org/1999/xhtml"
  2. <head id="Head1" runat="server"
  3.     <title>Untitled Page</title> 
  4. </head> 
  5. <body> 
  6.     <form id="form1" runat="server"
  7.         <asp:FileUpload ID="FileUpload1" runat="server" /> 
  8.         <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
  9.         <asp:Image ID="Image1" runat="server" />
  10.     </form> 
  11. </body> 
  12. </html>
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.Text;
  11. using System.IO;
  12. using System.Data.OleDb; 
  13. public partial class Default2 : System.Web.UI.Page
  14. {
  15.     private readonly string[] AcceptedFileTypes = new string[] { "jpg""jpeg""jpe""gif""bmp""png" };
  16.     protected void Page_Load(object sender, EventArgs e)
  17.     {
  18.         
  19.     }
  20.     public string GetPicturePath()
  21.     {
  22.         string savePath = string.Empty;
  23.         string strFileName = FileUpload1.PostedFile.FileName;
  24.         if (string.IsNullOrEmpty(strFileName))
  25.         {
  26.             return string.Empty;
  27.         }
  28.         else
  29.         {
  30.             int intIndex = strFileName.LastIndexOf("//");
  31.             string myFileName = strFileName.Substring(intIndex + 1);
  32.             savePath = HttpContext.Current.Server.MapPath("~/HeadImage") + "//" + myFileName;
  33.             FileUpload1.PostedFile.SaveAs(savePath);
  34.             string mypath = "~/HeadImage" + "//" + myFileName;
  35.             return mypath;
  36.         }
  37.     }
  38.     protected void Button1_Click(object sender, EventArgs e)
  39.     {
  40.         if (CheckPicture())
  41.         {
  42.             Image1.ImageUrl = GetPicturePath();
  43.         }
  44.     }
  45.     protected bool CheckPicture()
  46.     {
  47.         string strFileName = FileUpload1.PostedFile.FileName.ToLower();
  48.         if (string.IsNullOrEmpty(strFileName))
  49.         {
  50.             Page.ClientScript.RegisterStartupScript(GetType(), "JS""<script>alert('不能上傳空文件,請重新選擇!');</script>");
  51.             return false;
  52.         }
  53.         else
  54.         {
  55.             string strtype = strFileName.Substring(strFileName.LastIndexOf(".") + 1, strFileName.Length - strFileName.LastIndexOf(".") - 1);
  56.             for (int i = 0; i < AcceptedFileTypes.Length; i++)
  57.             {
  58.                 if (strtype == AcceptedFileTypes[i])
  59.                 {
  60.                     return true;
  61.                 }
  62.             }
  63.             Page.ClientScript.RegisterStartupScript(GetType(), "JS""<script>alert('上傳文件類型不符合要求,請重新選擇!');</script>");
  64.             return false;
  65.         }
  66.     }
  67. }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章