. NET MVC Backend accepts base64 upload images

Source: Internet
Author: User

 1 #region multiple images with front end upload 2 #region Upload image Method 3//<summary> 4//interface method 5//&lt ;/summary> 6//<param name= "img" > Base64 pictures with data:image/jpeg;base64 </param> 7//<re             turns> Picture Path </returns> 8 [HttpPost] 9 public actionresult GETADDIMG2 (String img) 10 {11             if (img = = null) {return Json ("empty");}15 Else16         {18 Return Json (Pduploadimage (IMG), jsonrequestbehavior.allowget);}19}20 #endregion21 #region Picture Conversion//<summary>23//Base64 string//</summary&gt ;///<param Name= "img" ></param>26///<returns> picture converted to 32 name </returns>27 PU Blic jsonresult pduploadimage (string img), {string error = ""; return Json (saveimage ("/upload/article", IMG, Ref error), Jsonrequestbehavior.allowget}32 #endregion33 #region intercept data:image/jpeg;base64, extract         Pictures and save Pictures///&LT;SUMMARY&GT;35///intercept data:image/jpeg;base64, extract pictures, and save Pictures///</summary>37 <param name= "file_name" ></param>38//<param name= "img_string" >base64 string </param >39//<param name= "error" > Wrong picture format </param>40//<returns> path + picture name </returns> $ private String SaveImage (string file_name, String img_string, ref string error) (//try //{45 string[] Img_array = img_string. Split (', '); byte[] arr = convert.frombase64string (img_array[1]); using (MemoryStream ms = new MemoryStream (arr)), Bitmap bmp = new Bitmap (ms), if (img_array[0]. ToLower () = = "Data:image/jpeg;base64") {BMP. SAve (file_name + ". jpg"), Setimg return (Guid.NewGuid (). ToString (). Replace ('-', ' P '). Substring (4), "JPG", arr);}55 else if (img_array[0]. ToLower () = = "Data:image/png;base64"). Save (file_name + ". png"), Setimg return (Guid.NewGuid (). ToString (). Replace ('-', ' P '). Substring (4), "PNG", arr),}60 else if (img_array[0]. ToLower () = = "Data:image/gif;base64"). Save (file_name + ". gif"), and Setimg return (Guid.NewGuid (). ToString (). Replace ('-', ' P '). Substring (4), "GIF", arr);}65 else66 {error = "No This file format is supported. "}70}71//}72//catch (exceptio n ex)//{74//error = "There was a problem generating the picture. "+ Ex.       ToString (); 75      return "wrong";//}77}78 #endregion79 #region Save picture Path and Settings name://&LT ; summary>81///Save to File path </summary>83///<param Name= "Imgname" > Saved file name </pa ram>84//<param name= "suffix" > Suffix </param>85//<param name= "arr" >base64</param& gt;86///<returns> Picture path </returns>87 public string setimg (string imgname, string suffix, byte[ ] arr. {STR3 string = system.appdomain.currentdomain.basedirectory;//Find relative path STR4 = system.appdomain.currentdomain.setupinformation.applicationbase;//Find relative path System.IO.File.WriteAllBytes (Str3 + "/upload/article/" + imgname + "." + suffix + "", arr); return "/upload/article/" + imgname + "." + suffix + "";}94 #endregion95 #endregion

. NET MVC Backend accepts base64 upload images

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.