1 #regionMultiple image uploads with front end2 #regionUpload Image method3 /// <summary>4 ///interface Method5 /// </summary>6 /// <param name= "img" >pictures of base64 with Data:image/jpeg;base64,</param>7 /// <returns>Picture Path</returns>8 [HttpPost]9 Publicactionresult GETaddimg2 (String img)Ten { One if(img = =NULL) A { - returnJson ("Empty"); - } the Else - { - returnJson (Pduploadimage (IMG), jsonrequestbehavior.allowget); - } + } - #endregion + #regionImage conversion A /// <summary> at ///string of base64 - /// </summary> - /// <param name= "img" ></param> - /// <returns>the picture turns into a 32 name</returns> - PublicJsonresult Pduploadimage (stringimg) - { in stringError =""; - returnJson (SaveImage ("/upload/article"Imgreferror), jsonrequestbehavior.allowget); to } + #endregion - #regionIntercept data:image/jpeg;base64, extract pictures, and save pictures the /// <summary> * ///intercept data:image/jpeg;base64, extract pictures, and save pictures $ /// </summary>Panax Notoginseng /// <param name= "file_name" ></param> - /// <param name= "img_string" >string of base64</param> the /// <param name= "error" >wrong picture format</param> + /// <returns>path + name of the picture</returns> A Private stringSaveImage (stringfile_name,stringImg_string,ref stringerror) the { + //Try - //{ $ string[] Img_array = img_string. Split (','); $ byte[] arr = convert.frombase64string (img_array[1]); - using(MemoryStream ms =NewMemoryStream (arr)) - { theBitmap BMP =NewBitmap (MS); - if(img_array[0]. ToLower () = ="data:image/jpeg;base64")Wuyi { theBmp. Save (file_name +". jpg"); - returnSetimg (Guid.NewGuid (). ToString (). Replace ('-','P'). Substring (4),"jpg", arr); Wu } - Else if(img_array[0]. ToLower () = ="data:image/png;base64") About { $Bmp. Save (file_name +". PNG"); - returnSetimg (Guid.NewGuid (). ToString (). Replace ('-','P'). Substring (4),"PNG", arr); - } - Else if(img_array[0]. ToLower () = ="data:image/gif;base64") A { +Bmp. Save (file_name +". gif"); the returnSetimg (Guid.NewGuid (). ToString (). Replace ('-','P'). Substring (4),"gif", arr); - } $ Else the { theError ="the file format is not supported. "; the return "wrong"; the } - } in //} the //catch (Exception ex) the //{ About //error = "A picture has been generated incorrectly. "+ Ex. ToString (); the //return "wrong"; the //} the } + #endregion - #regionSave picture Path and set name the /// <summary>Bayi ///Save to file path the /// </summary> the /// <param name= "Imgname" >saved file name</param> - /// <param name= "suffix" >suffix name</param> - /// <param name= "arr" >Base64</param> the /// <returns>the path to the picture</returns> the Public stringSetimg (stringImgname,stringSuffixbyte[] arr) the { the stringSTR3 = System.AppDomain.CurrentDomain.BaseDirectory;//Find relative path - stringSTR4 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//Find relative path theSystem.IO.File.WriteAllBytes (STR3 +"/upload/article/"+ Imgname +"."+ suffix +"", arr); the return "/upload/article/"+ Imgname +"."+ suffix +""; the }94 #endregion the #endregion
. NET MVC Backend accepts base64 upload images