javascript - 为什么图像上传器无法在 chrome 上运行?
问题描述
我有一个按钮,您可以在其中上传图像并将其插入标签中,它适用于每个浏览器,但不适用于 chrome。第一个reader.onloadend
已处理,我可以登录image
和image.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 处停止,但控制台中没有错误
解决方案
推荐阅读
- python - 如何在pytest中模拟sqlalchemy模型创建方法
- python - 如何通过订阅 ID 和 Azure 中的位置获取过滤后的 vm 大小列表
- regex - 正则表达式删除 js HTML 属性
- html - 根据一个参数按升序排列,并从数组 Angular 8 中删除前 6 个之后的所有对象
- java - 在 JavaFx 示例中找不到或加载主类
- string - 如何按特定长度(而不是分隔符)将字符串拆分为列
- sql - 用于查询 Access 数据库的 Excel VBA 用户定义函数
- javascript - 添加和删除类数组错误
- css - 带有修复页脚和导航的 Bootstrap 4 全屏页面:内容不会填充和溢出
- recursion - 不安全的 Rust 中的堆栈引用,但确保不安全不会从堆栈中泄漏?