This article mainly introduces C # 's most complete http://www.php.cn/php/php-tp-uploads.html "target=" _blank "> Upload image method, including the picture size limit, picture size, file content and so on judgment. Have a good reference value, follow the small series together to see it
The method includes the image size limit, the picture size, the file content and so on the judgment ...
The case is a demo under MVC that supports a single image upload.
Public ActionResult Upload () {String imgurl = ""; foreach (string key in Request.Files) {//Here only Test upload first picture file[0] httppostedfilebase File0 = request.files[ Key]; Convert to Byte, read picture MIME type stream stream; int size = File0. contentlength/1024; File size KB if (Size > 1024x768) {return Content (returnmsg (enum_return. Fail, "Picture cannot exceed 1M:", null)); } byte[] Filebyte = new Byte[2];//contentlength, here we only read the file length of the first two bits used to judge the good, so faster, the rest is not used. stream = File0. InputStream; Stream. Read (filebyte, 0, 2);//contentlength, or take top two bits//Get picture width and height//system.drawing.image image = System.Drawing.Image.Fr Omstream (stream); int width = image. Width; int height = image. Height; String fileflag = ""; if (filebyte! = null && filebyte.length > 0)//whether the picture data is empty {Fileflag = filebyte[0]. ToString () filebyte[1]. ToString (); } string[] Filetypestr = {"255216", "7173", "6677", "13780"};//the corresponding picture format 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. Fail, "Picture is malformed:" Fileflag, null)); } stream. Close (); } return Content (Returnmsg (Enum_return. Success, "Upload succeeded", Imgurl)); }
Generic Handler
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 ("Please upload pictures"); } 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") {_UPF Ile. SaveAs (HttpContext.Current.Server.MapPath ("photo") "\ \" Dataname "." Type); HttpCookie cookie = new HttpCookie ("photo"); Context. Response.Write ("Upload success"); } else {context. Response.Write ("Supported format: |jpg|gif|bmp|"); }} else {context.Response.Write ("Your picture is already over 500K in size!") "); } }