C# 最齊全的上傳圖片方法
更新時間:2017年01月22日 08:56:14 作者:先修客
本文主要介紹了C# 最齊全的上傳圖片方法,方法里包括了圖片大小限制、圖片尺寸、文件內(nèi)容等等的判斷。具有很好的參考價值,下面跟著小編一起來看下吧
方法里包括了圖片大小限制、圖片尺寸、文件內(nèi)容等等的判斷。。。
該案例是mvc下的demo,支持單張圖片上傳。
public ActionResult Upload()
{
string imgurl = "";
foreach (string key in Request.Files)
{
//這里只測試上傳第一張圖片file[0]
HttpPostedFileBase file0 = Request.Files[key];
//轉(zhuǎn)換成byte,讀取圖片MIME類型
Stream stream;
int size = file0.ContentLength / 1024; //文件大小KB
if (size > 1024)
{
return Content(ReturnMsg(Enum_Return.失敗, "圖片不能超過1M:", null));
}
byte[] fileByte = new byte[2];//contentLength,這里我們只讀取文件長度的前兩位用于判斷就好了,這樣速度比較快,剩下的也用不到。
stream = file0.InputStream;
stream.Read(fileByte, 0, 2);//contentLength,還是取前兩位
//獲取圖片寬和高
//System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
//int width = image.Width;
//int height = image.Height;
string fileFlag = "";
if (fileByte != null && fileByte.Length > 0)//圖片數(shù)據(jù)是否為空
{
fileFlag = fileByte[0].ToString() fileByte[1].ToString();
}
string[] fileTypeStr = { "255216", "7173", "6677", "13780" };//對應(yīng)的圖片格式j(luò)pg,gif,bmp,png
if (fileTypeStr.Contains(fileFlag))
{
string action = Request["action"];
string path = "/uploads/";
switch (action)
{
case "headimage":
path = "headimage/";
break;
case "blogtype":
path = "blogtype/";
break;
}
string fullpath = path UserInfo.userID "/";
if (!Directory.Exists(Server.MapPath(fullpath)))
{
Directory.CreateDirectory(Server.MapPath(fullpath));
}
Request.Files[key].SaveAs(Server.MapPath(fullpath Request.Files[key].FileName));
imgurl = fullpath Request.Files[key].FileName;
}
else
{
return Content(ReturnMsg(Enum_Return.失敗, "圖片格式不正確:" fileFlag, null));
}
stream.Close();
}
return Content(ReturnMsg(Enum_Return.成功, "上傳成功", imgurl));
}
一般處理程序
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
HttpPostedFile _upfile = context.Request.Files["File"];
if (_upfile.ContentLength < 500000)
{
if (string.IsNullOrEmpty(_upfile.FileName))
{
context.Response.Write("請上傳圖片");
}
string fileFullname = _upfile.FileName;
string dataName = DateTime.Now.ToString("yyyyMMddhhmmss");
string fileName = fileFullname.Substring(fileFullname.LastIndexOf("\\") 1);
string type = fileFullname.Substring(fileFullname.LastIndexOf(".") 1);
if (type == "bmp" || type == "jpg" || type == "gif" || type == "JPG" || type == "BMP" || type == "GIF")
{
_upfile.SaveAs(HttpContext.Current.Server.MapPath("photo") "\\" dataName "." type);
HttpCookie cookie = new HttpCookie("photo");
context.Response.Write("上傳成功");
}
else
{
context.Response.Write("支持格式:|jpg|gif|bmp|");
}
}
else
{
context.Response.Write("你的圖片已經(jīng)超過500K的大??!");
}
}
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關(guān)文章
C#編程自學(xué)之?dāng)?shù)據(jù)類型和變量二
這篇文章繼續(xù)介紹了C#數(shù)據(jù)類型和變量,是對上一篇文章的補(bǔ)充,希望對大家的學(xué)習(xí)有所幫助。2015-10-10
VisualStudio2019安裝C#環(huán)境的實現(xiàn)方法
這篇文章主要介紹了VisualStudio2019安裝C#環(huán)境的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
2020-12-12
12306奇葩驗證碼引發(fā)思考之C#實現(xiàn)驗證碼程序
春運(yùn)最高峰來了!明天通過網(wǎng)絡(luò)將能買到小年夜的車票,本周四就將開售除夕日車票,但不少人被首次在春運(yùn)期間使用的圖片驗證碼搞得很火大,小編也正在對驗證碼進(jìn)行研究,編寫了由C#實現(xiàn)驗證碼程序,分享給大家
2015-12-12
C#如何將查詢到的數(shù)據(jù)庫里面的數(shù)據(jù)輸出到textbox控件
這篇文章主要介紹了C#如何將查詢到的數(shù)據(jù)庫里面的數(shù)據(jù)輸出到textbox控件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
2023-07-07
C#數(shù)據(jù)結(jié)構(gòu)之最小堆的實現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于C#數(shù)據(jù)結(jié)構(gòu)之最小堆的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
2021-02-02 
