Firefox convertImgToDataURLviaCanvas不适用于Firefox,但在Chrome和IE中工作

时间:2016-05-03 04:33:56

标签: image firefox canvas

我的以下功能在firefox中无效,但在chrome

中有效
function convertImgToDataURLviaCanvas(url, callback, outputFormat){
    var img = new Image();
    img.crossOrigin = 'Anonymous';
    img.onload = function(){
        var canvas = document.createElement('CANVAS');
        var ctx = canvas.getContext('2d');
        var dataURL;
        canvas.height = this.height;
        canvas.width = this.width;
        ctx.drawImage(this, 0, 0);
        dataURL = canvas.toDataURL(outputFormat);
        callback(dataURL);
        canvas = null; 
    };
    img.src = url;
}

我必须将base64编码传递给web服务,我在firefox中获取的编码字符串在webservice结束时失败,但是我在chrome和IE中获得的字符串正在工作。以下是在Imageupload上调用的代码

  $("#avatar1").on('change',function(event){
  var selectedFile = this.files[0];
  var tmppath = URL.createObjectURL(event.target.files[0]);
 // $('.profile-image').append('<img src="'+tmppath+'" />');
  var convertFunction = convertImgToDataURLviaCanvas;
    convertFunction(tmppath, function(base64Img){
            console.log(base64Img);
        var imagebase64Obj = new Object();
        imagebase64Obj.data = base64Img;


        $.ajax({
                url : "/bin/mercer/saml/myProfileController?q=updateavatar",
                type : "POST",
                data : imagebase64Obj,
                success : function(data, textStatus, jqXHR) {
                // data - response from server
                    window.location.reload();
                alert("success");
            },
                  error : function(jqXHR, textStatus,
                  errorThrown) {
                alert("error");
            }
        });
    });



});

请帮助我解决出错的问题,或者在firefox中使用的其他变体函数

0 个答案:

没有答案