首页 > 解决方案 > 为什么图像上传器无法在 chrome 上运行?

问题描述

我有一个按钮,您可以在其中上传图像并将其插入标签中,它适用于每个浏览器,但不适用于 chrome。第一个reader.onloadend已处理,我可以登录imageimage.src.

var inputUpload = document.getElementById('buttonUpload');

inputUpload.addEventListener('change', (e) => {
  const file = e.target.files[0];
  const reader = new FileReader();

  reader.onloadend = () => {
  //Initiate the JavaScript Image object.
  var image = new Image();

  image.src = reader.result;
  console.log('image.src');

  image.onloadend = function() { /* execution stops here */
    var height = this.height;
    var width = this.width;
    if (height > 150 || width > 150) {
      showAlert('Image dimensions must be within 150×150 pixels.');
    } else {
      // convert file to base64 String
      const base64String = reader.result.replace('data:', '').replace(/^.+,/, '');
      // display image
      valueImg = "data:image/png;base64," + base64String;
      updateImg(valueImg);
    };
  };
};
reader.readAsDataURL(file);
});
<button id="buttonUpload" type="button" class="d-inline btn-custom mx-0">
     Upload
</button>

    

当我选择图像文件时,它会在 image.loadend 处停止,但控制台中没有错误

标签: javascript

解决方案


推荐阅读