关于 javascript抓取网络图片转成file上传

 关于 javascript抓取网络图片转成file上传


    var c = document.createElement("canvas");
    var cxt=c.getContext("2d");
    var img=new Image();
    img.setAttribute("crossOrigin",'Anonymous');
    img.onload=function(){
        var fileIndex=this.src.lastIndexOf("/");
        var filename= this.src.substr(fileIndex+1);

        c.width = this.width;
        c.height = this.height;
        cxt.drawImage(img, 0, 0, this.width, this.height) // 在canvas上绘制图片
        var dataURL = compress(img,this.width,this.height,0.8);
        /*为了兼容ios 需要 dataURL-> blob -> file */
        var blob = dataURLtoBlob(dataURL);		// dataURL-> blob
        var file = blobToFile(blob, filename);		// blob -> file
        console.log("得到文件:"+file);
        console.log("压缩后大小:"+file.size/1024);


        var fd = new FormData();
        fd.append("upfile",file  ,filename);
        //fd.append("files",file ,filename);
        $.ajax({
            //url: 'http://127.0.0.1:8080/base/file/upload',
            url:'http://127.0.0.1:8081/ueditor/config?action=uploadimage',
            type : 'POST',
            dataType : 'json',
            cache : false,
            contentType : false,//(必须这样配置)
			processData : false,    //JQuery不处理发送数据
// 			contentType : 'multipart/form-data',(如果这样,会导致contentType没有边界boundary,导致文件解析失败,后台报错Could not parse multipart servlet request;)
            data: fd,
            success: function (res) {
                debugger
                if(res.code==0){
                    console.log(res.data[0].savePath+'上传成功\n');
                }else{
                    console.error(filename+'上传失败\n');
                }
            },error:function(res){
                console.error(filename+'上传失败\n');
            }
        })


    }

    img.src="https://img.alicdn.com/imgextra/i4/6000000004742/O1CN0186kDCj1ktshI25kK9_!!6000000004742-0-try-report.jpg";

    function compress(img, width, height, ratio) {
        // img可以是dataURL或者图片url
        /*	如果宽度大于 750 图片太大 等比压缩 	*/
        if(width > 750){
            var ratio = width/height;
            width = 750;
            height = 750/ratio;
        }
        var canvas, ctx, img64;
        canvas = document.createElement('canvas');
        canvas.width = width;
        canvas.height = height;


        ctx = canvas.getContext("2d");
        ctx.drawImage(img, 0, 0, width, height);
        /* canvas.toDataURL(mimeType(图片的类型), qualityArgument(图片质量)) */
        img64 = canvas.toDataURL("image/jpeg", ratio);

        return img64; // 压缩后的base64串
    }
    //将base64转换为blob
    function dataURLtoBlob (dataurl) {
        var arr = dataurl.split(','),
            mime = arr[0].match(/:(.*?);/)[1],
            bstr = atob(arr[1]),
            n = bstr.length,
            u8arr = new Uint8Array(n);
        while (n--) {
            u8arr[n] = bstr.charCodeAt(n);
        }
        return new Blob([u8arr], { type: mime });
    }
    //将blob转换为file
    function blobToFile(theBlob, fileName){
        theBlob.lastModifiedDate = new Date();
        theBlob.name = fileName;
        return theBlob;
    }

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值