. NET MVC Backend accepts base64 upload images

Source: Internet
Author: User

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

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.