C#檢測上傳文件真正類型的辦法。本站提示廣大學習愛好者:(C#檢測上傳文件真正類型的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#檢測上傳文件真正類型的辦法正文
本文實例講述了C#檢測上傳文件真正類型的辦法。分享給年夜家供年夜家參考。詳細剖析以下:
關於用戶上傳的文件假如只是依據擴大名斷定,很輕易上傳下去可履行文件,這長短常風險的,這段代碼可以在辦事器端檢測上傳文件的真實類型。
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> void Alert(string s) { Page.ClientScript.RegisterStartupScript(Page.GetType(), "js", "alert('" + s + "')", true); } protected void Button1_Click(object sender, EventArgs e) { saveFile(); } protected String saveFile() { String MaxSize = "1024"; //最年夜文件年夜小 int imgMaxSize = Convert.ToInt32(MaxSize) * 1024 * 1024; HttpPostedFile imgFile = FuImg.PostedFile; if (imgFile == null || FuImg.FileName == "") { Alert("請選擇文件。"); return ""; } String dirPath = Server.MapPath("~/"); string saveUrl = Page.ResolveUrl("~/"); if (!System.IO.Directory.Exists(dirPath)) { Alert("上傳目次不存在。"); return ""; } String fileName = imgFile.FileName; String fileExt = System.IO.Path.GetExtension(fileName).ToLower(); if (imgFile.InputStream == null || imgFile.InputStream.Length > imgMaxSize) { Alert("上傳文件年夜小跨越限制。"); return ""; } //驗證文件格局 String fpath = IsAllowedExtension(imgFile); if ("" == fpath) { Alert("圖片格局不准確。"); return ""; } String ymd = DateTime.Now.ToString("yyyyMMdd", System.Globalization.DateTimeFormatInfo.InvariantInfo); dirPath += ymd + "/"; saveUrl = saveUrl + ymd + "/"; //斷定目次能否存在 if (!System.IO.Directory.Exists(dirPath)) { //創立目次 System.IO.Directory.CreateDirectory(dirPath); } String newFileName = Guid.NewGuid().ToString() + fileExt; //圖片名字 String filePath = dirPath + newFileName; System.IO.File.Move(fpath, filePath); String fileUrl = saveUrl + newFileName; Img.ImageUrl = fileUrl; //ImageUrl = saveUrl + newFileName; return fileUrl; } public String IsAllowedExtension(HttpPostedFile f) { String newFile = Server.MapPath("~/" + System.Guid.NewGuid().ToString("D") + ".tmp"); f.SaveAs(newFile); System.IO.FileStream fs = new System.IO.FileStream(newFile, System.IO.FileMode.Open, System.IO.FileAccess.Read); System.IO.BinaryReader r = new System.IO.BinaryReader(fs); string fileclass = ""; byte buffer; buffer = r.ReadByte(); fileclass = buffer.ToString(); buffer = r.ReadByte(); fileclass += buffer.ToString(); r.Close(); fs.Close(); /* 文件擴大名解釋 *7173 gif *255216 jpg *13780 png *6677 bmp */ Dictionary<String, String> ftype = new Dictionary<string, string>(); //添加許可的文件類型 ftype.Add("7173", "gif"); ftype.Add("255216", "jpg"); ftype.Add("13780", "png"); ftype.Add("6677", "bmp"); if (ftype.ContainsKey(fileclass)) { return newFile; } else { System.IO.File.Delete(newFile); return ""; } } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> </head> <body> <form id="form1" runat="server"> <asp:FileUpload ID="FuImg" runat="server" /> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上傳測試" /> <asp:Image ID="Img" runat="server" /> </form> </body> </html>
願望本文所述對年夜家的C#法式設計有所贊助。