C#檢測(cè)上傳文件真正類型的方法
本文實(shí)例講述了C#檢測(cè)上傳文件真正類型的方法。分享給大家供大家參考。具體分析如下:
對(duì)于用戶上傳的文件如果只是根據(jù)擴(kuò)展名判斷,很容易上傳上來(lái)可執(zhí)行文件,這是非常危險(xiǎn)的,這段代碼可以在服務(wù)器端檢測(cè)上傳文件的真實(shí)類型。
<%@ 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("請(qǐng)選擇文件。");
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("上傳文件大小超過(guò)限制。");
return "";
}
//驗(yàn)證文件格式
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))
{
//創(chuàng)建目錄
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();
/* 文件擴(kuò)展名說(shuō)明
*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="上傳測(cè)試" />
<asp:Image ID="Img" runat="server" />
</form>
</body>
</html>
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
- C# 使用Fiddler捕獲本地HttpClient發(fā)出的請(qǐng)求操作
- C# 使用HttpClient模擬請(qǐng)求的案例
- C#中HttpWebRequest、WebClient、HttpClient的使用詳解
- C# HttpClient Cookie驗(yàn)證解決方法
- c# FTP上傳文件實(shí)例代碼(簡(jiǎn)易版)
- C#實(shí)現(xiàn)的文件上傳下載工具類完整實(shí)例【上傳文件自動(dòng)命名】
- C#遍歷文件夾后上傳文件夾中所有文件錯(cuò)誤案例分析
- C#采用HttpWebRequest實(shí)現(xiàn)保持會(huì)話上傳文件到HTTP的方法
- C# 使用HttpClient上傳文件并附帶其他參數(shù)的步驟
相關(guān)文章
C# WinForm國(guó)際化實(shí)現(xiàn)的簡(jiǎn)單方法
這篇文章主要介紹了C# WinForm國(guó)際化實(shí)現(xiàn)的簡(jiǎn)單方法,有需要的朋友可以參考一下2014-01-01
VS中模仿WPF模板創(chuàng)建最簡(jiǎn)單的WPF程序
這篇文章主要為大家詳細(xì)介紹了VS中模仿WPF模板創(chuàng)建最簡(jiǎn)單的WPF程序的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-05-05
c# 兩個(gè)數(shù)組比較,將重復(fù)部分去掉,返回不重復(fù)部分的實(shí)現(xiàn)
下面小編就為大家?guī)?lái)一篇c# 兩個(gè)數(shù)組比較,將重復(fù)部分去掉,返回不重復(fù)部分的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12
C#設(shè)計(jì)模式之ChainOfResponsibility職責(zé)鏈模式解決真假美猴王問(wèn)題實(shí)例
這篇文章主要介紹了C#設(shè)計(jì)模式之ChainOfResponsibility職責(zé)鏈模式解決真假美猴王問(wèn)題,簡(jiǎn)單說(shuō)明了責(zé)任鏈模式的概念,并結(jié)合《西游記》中真假美猴王故事背景為實(shí)例分析了責(zé)任鏈模式的具體使用技巧,需要的朋友可以參考下2017-09-09
C#使用Clipboard類實(shí)現(xiàn)剪貼板功能
這篇文章介紹了C#使用Clipboard類實(shí)現(xiàn)剪貼板功能的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
C#利用DesignSurface如何實(shí)現(xiàn)簡(jiǎn)單的窗體設(shè)計(jì)器
這篇文章主要介紹了C#利用DesignSurface如何實(shí)現(xiàn)簡(jiǎn)單窗體設(shè)計(jì)器的相關(guān)資料,文中通過(guò)圖文及示例代碼介紹的很詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-02-02

