国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C# 最齊全的上傳圖片方法

2019-10-29 21:15:30
字體:
供稿:網(wǎng)友

方法里包括了圖片大小限制、圖片尺寸、文件內(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" };//對應的圖片格式jpg,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)容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持VEVB武林網(wǎng)!


注:相關(guān)教程知識閱讀請移步到c#教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 天水市| 海宁市| 东源县| 湘西| 淮安市| 佛学| 平罗县| 东丰县| 石门县| 古交市| 衡南县| 汉源县| 桦川县| 东辽县| 南昌县| 永济市| 黄陵县| 稷山县| 桦甸市| 玉山县| 儋州市| 仙居县| 忻州市| 南汇区| 忻城县| 乌恰县| 十堰市| 兰州市| 乌拉特中旗| 肥乡县| 黎城县| 南皮县| 无棣县| 建瓯市| 璧山县| 林州市| 堆龙德庆县| 合阳县| 合水县| 怀安县| 尼勒克县|