图片采用base64压缩,可以以字符串的形式传送base64给服务端转存为图片

(function () {
    var coverImage = document.querySelector('<div id="coverImage">file</div>');
    //图片压缩
    coverImage.onchange = function () {
        lrz(this.files[0], {width: 640}, function (results) {
            
            // 你需要的数据都在这里,可以以字符串的形式传送base64给服务端转存为图片。
            var base64 = results.base64;
            function base64UrlToBlob(base64){
                var bytes=window.atob(base64.split(',')[1]);        //去掉url的头,并转换为byte
                //处理异常,将ascii码小于0的转换为大于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对象
            }
            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('预压的图片', base64, base64.length * 0.5);
            }, 100);
            
        });
    };
})();

 

 (function () {
    var coverImage = document.querySelector('<div id="coverImage">file</div>');
    //图片压缩
    coverImage.onchange = function () {
        lrz(this.files[0], {width: 640}, function (results) {
        
            // 你需要的数据都在这里,可以以字符串的形式传送base64给服务端转存为图片。
            var base64 = results.base64;
            function base64UrlToBlob(base64){
                var bytes=window.atob(base64.split(',')[1]);        //去掉url的头,并转换为byte
                //处理异常,将ascii码小于0的转换为大于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对象
            }
            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('预压的图片', base64, base64.length * 0.5);
            }, 100);
            
        });
    };
})();
posted @ 2016-10-22 23:47  春风得意之时  阅读(4177)  评论(0编辑  收藏  举报