The image is base64 compressed, can be sent as a string base64 to the server to dump the image

Source: Internet
Author: User

(function () {    varCoverimage = Document.queryselector (' <div id= ' coverimage ' >file</div> '); //image CompressionCoverimage.onchange =function() {Lrz ( This. files[0], {width:640},function(results) {//The data you need is here, and can be sent as a string base64 to the server dump as a picture.             varBase64 =results.base64; functionBase64urltoblob (base64) {varBytes=window.atob (Base64.split (', ') [1]);//remove the header from the URL and convert it to byte                //handling exceptions, converting ASCII codes less than 0 to greater than 0                varAB =NewArrayBuffer (bytes.length); varIA =NewUint8array (AB);  for(vari = 0; i < bytes.length; i++) {Ia[i]=bytes.charcodeat (i); }                return NewBlob ([ab], {type: ' image/jpg '});//return Blob Object            }            varFormData =NewFormData ($ ("#toPic") [1]); varBlob =Base64urltoblob (base64);            Console.log (BLOB); Formdata.append ("Coverimage", BLOB); $.ajax ({URL:"XXX", type:"POST", Data:formdata, DataType:"JSON", ContentType:false, ProcessData:false, Success:function(JSON) {console.log (JSON);            },            }); SetTimeout (function() {Demo_report (' Pre-pressure pictures ', base64, Base64.length * 0.5); }, 100);    }); };}) ();

(function () {
var coverimage = document.queryselector (' <div id= ' coverimage ' >file</div> ');
Image compression
Coverimage.onchange = function () {
Lrz (This.files[0], {width:640}, function (results) {

The data you need is here, and can be sent as a string base64 to the server dump as a picture.
var base64 = results.base64;
function Base64urltoblob (base64) {
var Bytes=window.atob (Base64.split (', ') [1]); Remove the header from the URL and convert it to byte
Handling exceptions, converting ASCII codes less than 0 to 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/jpg '}); Return BLOB Object
}
var formData = new FormData ($ ("#toPic") [1]);
var blob = Base64urltoblob (base64);
Console.log (BLOB);
Formdata.append ("Coverimage", blob);
$.ajax ({
URL: "XXX",
Type: "POST",
Data:formdata,
DataType: "JSON",
Contenttype:false,
Processdata:false,
Success:function (JSON) {
Console.log (JSON);
},
});
SetTimeout (function () {
Demo_report (' Pre-pressure pictures ', base64, base64.length * 0.5);
}, 100);

});
};
})();

The image is base64 compressed, can be sent as a string base64 to the server to dump the image

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.