Upload image file converted to blob

Source: Internet
Author: User

Directly submit Base64 encoded image data, too large, there will be a forwarding error in the background. A good solution is to convert the Base64 encoded picture data to a blob (similar to file) and add it to the form for submission. Here's the code:
/** * @param base64codes * Image base64 encoded */function Sumitimagefile (base64codes) {var formData = new FormData () ;    Here the other parameters in the form are also submitted together, if you do not need to submit additional parameters can be directly formdata parameterless constructor var url = "Upload url";  The Convertbase64urltoblob function converts the Base64 encoding to BLOB formdata.append ("ImageName", Convertbase64urltoblob (Base64codes)); The first parameter of the APPEND function is to get the parameter name of the data in the background, and the Name property of the input of the HTML tag is the same as the//ajax submit form if (window.                XMLHttpRequest) {xmlhttp = new XMLHttpRequest (); } else if (window.                ActiveXObject) {xmlhttp = new ActiveXObject ();                } xmlhttp.open ("POST", url, True);                Xmlhttp.send (FormData); Xmlhttp.onload = function (e) {if (This.status = =) {var obj = eval ("+ thi                        S.responsetext + ")");                                          var re = obj["return"]; }                };}      /** * Convert image URL data in base64 to BLOB * @param urldata *      Base64 picture data represented by URL */function convertbase64urltoblob (urldata) {var Bytes=window.atob (urldata.split (', ') [1]);    Remove the header of the URL and convert it to a byte//handle exception, converting the ASCII code less than 0 to a greater than 0 var ab = new ArrayBuffer (bytes.length);    var ia = new Uint8array (AB);    for (var i = 0; i < bytes.length; i++) {Ia[i] = bytes.charcodeat (i); } return new Blob ([ab], {type: ' Image/png '});}

  


Upload image file converted to blob

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.