C# 最齊全的上傳圖片方法
方法里包括了圖片大小限制、圖片尺寸、文件內(nèi)容等等的判斷。。。
該案例是mvc下的demo,支持單張圖片上傳。
public ActionResult Upload() { string imgurl = ""; foreach (string key in Request.Files) { //這里只測(cè)試上傳第一張圖片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.失敗, "圖片不能超過(guò)1M:", null)); } byte[] fileByte = new byte[2];//contentLength,這里我們只讀取文件長(zhǎng)度的前兩位用于判斷就好了,這樣速度比較快,剩下的也用不到。 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" };//對(duì)應(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("請(qǐng)上傳圖片"); } 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)超過(guò)500K的大小!"); } }
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持我們!
上一篇:C#實(shí)現(xiàn)XML文檔的增刪改查功能示例
欄 目:C#教程
下一篇:C#獲取ListView鼠標(biāo)下的Item實(shí)例
本文標(biāo)題:C# 最齊全的上傳圖片方法
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5996.html
您可能感興趣的文章
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并打開(kāi)的方法
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#停止線程的方法
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新聞效果的方法
- 01-10C#通過(guò)重寫Panel改變邊框顏色與寬度的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-11Mac OSX 打開(kāi)原生自帶讀寫NTFS功能(圖文
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置