The. net mvc background accepts base64 upload images,
1 # region and Multiple front-end images upload 2 # region Image Upload method 3 /// <summary> 4 // Interface Method 5 /// </summary> 6 /// <param name = "img"> with data: image/jpeg; base64, base64 image </param> 7 // <returns> image path </returns> 8 [HttpPost] 9 public ActionResult getaddimg (String img) 10 {11 if (img = null) 12 {13 return Json ("null"); 14} 15 else16 {17 return Json (PDUploadImage (img), JsonRequestBehavior. allowGet); 18} 19} 20 # endregion21 # region image Conversion 22 /// <summary> 23 // base64 string 24 /// </summary> 25 /// <param name = "img"> </param> 26/ // <returns> convert the image to the 32 name </returns> 27 public JsonResult PDUploadImage (string img) 28 {29 string error = ""; 30 return Json (SaveImage ("/Upload/Article", img, ref error), JsonRequestBehavior. allowGet); 31} 32 # endregion33 # region truncate data: image/jpeg; base64, extract the image, and save the image 34 // <summary> 35 // truncate data: image/jpeg; base64: extract and save the image Picture 36 /// </summary> 37 /// <param name = "file_name"> </param> 38 /// <param name = "img_string"> base64 string </param> 39 // <param name = "error"> incorrect image format </param> 40 /// <returns> path + image name </returns> 41 private string SaveImage (string file_name, string img_string, ref string error) 42 {43 // try44 // {45 string [] img_array = img_string.Split (','); 46 byte [] arr = Convert. fromBase64String (img_array [1]); 47 using (Mem OryStream MS = new MemoryStream (arr) 48 {49 Bitmap bmp = new Bitmap (MS); 50 if (img_array [0]. toLower () = "data: image/jpeg; base64") 51 {52 bmp. save (file_name + ". jpg "); 53 return SetImg (Guid. newGuid (). toString (). replace ('-', 'P '). substring (4), "jpg", arr); 54} 55 else if (img_array [0]. toLower () = "data: image/png; base64") 56 {57 bmp. save (file_name + ". png "); 58 return SetImg (Guid. newGuid (). toString (). re Place ('-', 'P '). substring (4), "png", arr); 59} 60 else if (img_array [0]. toLower () = "data: image/gif; base64") 61 {62 bmp. save (file_name + ". gif "); 63 return SetImg (Guid. newGuid (). toString (). replace ('-', 'P '). substring (4), "gif", arr); 64} 65 else66 {67 error = "this file format is not supported. "; 68 return" error "; 69} 70} 71 //} 72 // catch (Exception ex) 73 // {74 // error =" An error occurred while generating the image. "+ Ex. toString (); 75 // return "error "; 76 //} 77} 78 # endregion79 # region Save the image path and set the name to 80 /// <summary> 81 // save it to the file path 82 /// </summary> 83 // <param name = "ImgName"> name of the saved file </param> 84 // <param name = "suffix"> suffix </param> 85 // /<param name = "arr"> base64 </param> 86 // <returns> path of the image </returns> 87 public string SetImg (string ImgName, string suffix, byte [] arr) 88 {89 string str3 = System. appDomain. currentDomain. baseDirectory; // find the relative path 90 string str4 = System. appDomain. currentDomain. setupInformation. applicationBase; // locate the relative path 91 System. IO. file. writeAllBytes (str3 + "/Upload/Article/" + ImgName + ". "+ suffix +" ", arr); 92 return"/Upload/Article/"+ ImgName + ". "+ suffix +" "; 93} 94 # endregion95 # endregion