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

來源:互聯網
上載者:User
本文主要介紹了C# 最齊全的http://www.php.cn/php/php-tp-uploads.html" target="_blank">上傳圖片方法,方法裡包括了圖片大小限制、圖片尺寸、檔案內容等等的判斷。具有很好的參考價值,下面跟著小編一起來看下吧

方法裡包括了圖片大小限制、圖片尺寸、檔案內容等等的判斷。。。

該案例是mvc下的demo,支援單張圖片上傳。

public ActionResult Upload()    {      string imgurl = "";      foreach (string key in Request.Files)      {        //這裡只測試上傳第一張圖片file[0]        HttpPostedFileBase file0 = Request.Files[key];        //轉換成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)//圖片資料是否為空白        {          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("你的圖片已經超過500K的大小!");    }  }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.