javascript - 在 Firefox 中,onload 未使用 createObjectURL img.src 触发

标签 javascript html canvas blob filereader

我有以下代码。它在 Chrome 中工作得很好,正如预期的那样。但是,在 Firefox 中,它记录“CONVERT”但从不记录“LOADED”。没有 JS 错误或任何东西。 onload 只是不触发。我似乎无法在 google 或 stackoverflow 上找到太多。许多人说 onload 不会为缓存的图像触发,但这些不应该被缓存,即使它们被缓存,我也无法缓存它们(对吗?)

  flattenImage: function(file, callback){
    // Safari uses webkitURL
    var URL = window.URL || window.webkitURL;
    var canPreformat = !!(window.Blob && window.Uint8Array && URL && URL.createObjectURL);

    // If we have all features we need to preformat on the client and the image
    // isn't already flattened (jpeg), DO IT
    if (canPreformat && !file.type.test(/jpe?g/i)) {
      console.log('CONVERT');
      var thiz = this;
      var c = document.createElement('canvas');
      var ctx = c.getContext('2d');
      var img = new Image;
      // Makes a blob URL from the file given.
      img.onload = function() {
        console.log('LOADED');
        c.width = this.width;
        c.height = this.height;

        // Take the img that was added and copy it to the canvas
        ctx.drawImage(img, 0, 0);

        // Put the image on top of everything else
        ctx.globalCompositeOperation = 'destination-atop';

        // Any transparency should become white (instead of the default black)
        ctx.fillStyle = "#fff";
        ctx.fillRect(0, 0, this.width, this.height);

        // Save canvas as a base64'd jpeg
        var dataURL = c.toDataURL("image/jpeg");

        // The following blob lines take the base64 encoded image and then
        // convert it to a jpeg "file". This allows us to do a real file
        // upload rather than needing to send a base64 string.
        var blobBin = atob(dataURL.split(',')[1]);
        var array = [];
        for(var i = 0; i < blobBin.length; i++) {
          array.push(blobBin.charCodeAt(i));
        }
        callback.call(thiz, new Blob([new Uint8Array(array)], {type: 'image/jpeg'}));
      }
      img.src = URL.createObjectURL(file);
    }
    // If we don't have all the features just return the same file given to us
    else {
      console.log('NOPE')
      callback.call(this, file);
    }
  }

最佳答案

我通过不使用 createObjectURL 而是像这样使用 FileReader 来解决这个问题:

flattenImage: function(file, callback){
    // Safari uses webkitURL
    var URL = window.URL || window.webkitURL;
    var canPreformat = !!(window.FileReader && window.Blob && window.Uint8Array);

    // If we have all features we need to preformat on the client and the image
    // isn't already flattened (jpeg), DO IT
    if (canPreformat && !file.type.test(/jpe?g/i)) {
      console.log('CONVERT');
      var thiz = this;
      var c = document.createElement('canvas');
      var ctx = c.getContext('2d');

      var reader = new FileReader();
      var img = new Image;

      // Once the image is loaded from FileReader set the src of the image to
      // the base64'd result. This will trigger the img.onload
      reader.onload = function (ev) {
        img.src = ev.target.result;
      };

      // Makes a blob URL from the file given.
      img.onload = function() {
        console.log('LOADED');
        c.width = this.width;
        c.height = this.height;

        // Take the img that was added and copy it to the canvas
        ctx.drawImage(img, 0, 0);

        // Put the image on top of everything else
        ctx.globalCompositeOperation = 'destination-atop';

        // Any transparency should become white (instead of the default black)
        ctx.fillStyle = "#fff";
        ctx.fillRect(0, 0, this.width, this.height);

        // Save canvas as a base64'd jpeg
        var dataURL = c.toDataURL("image/jpeg");

        // The following blob lines take the base64 encoded image and then
        // convert it to a jpeg "file". This allows us to do a real file
        // upload rather than needing to send a base64 string.
        var blobBin = atob(dataURL.split(',')[1]);
        var array = [];
        for(var i = 0; i < blobBin.length; i++) {
          array.push(blobBin.charCodeAt(i));
        }
        callback.call(thiz, new Blob([new Uint8Array(array)], {type: 'image/jpeg'}));
      }
      reader.readAsDataURL(file);
    }
    // If we don't have all the features just return the same file given to us
    else {
      callback.call(this, file);
    }
  }

关于javascript - 在 Firefox 中,onload 未使用 createObjectURL img.src 触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20668351/

相关文章:

javascript - AngularJS 路由问题

javascript - 响应式 <img> 标签检索错误的 src

javascript - 如何在 Canvas 上制作线条动画

android - 使用 Canvas 绘制时分别更改文本高度和宽度

javascript - JQuery Flot - 网格线不通过填充线显示

javascript - 根据数组或对象项的数量设置背景图像

javascript - 如何在 jQuery/Javascript 中固定/取消固定下拉菜单?

python - 使用 beutifulsoup 和 Mechanize 从 html 表中获取文本时出错

javascript - 糟糕的 Canvas GetImageData()/PutImageData() 在移动设备上的性能

javascript - javaScript 不是 DOM 的一部分吗?