javascript - 为什么 javascript 事件在 firefox 上不起作用?
问题描述
我有一个输入类型的文件,一旦用户点击上传他的图片,上传过程就会开始并显示该图片的小缩略图,这在 chrome 上正常工作,firefox 有问题,之后没有缩略图显示点击上传,这是我用来制作这个过程的代码:
var input = event.target;
// Instantiate FileReader
var reader = new FileReader();
reader.onload = function(){
TheFileContents = reader.result;
// Update the output to include the <img> tag with the data URL as the source
document.getElementById("img").src = TheFileContents;
};
// Produce a data URL (base64 encoded string of the data in the file)
// We are retrieving the first file from the FileList object
reader.readAsDataURL(input.files[0]);
解决方案
更新后的值由事件属性提供给 onload:event.target.result
var input = event.target;
// Instantiate FileReader
var reader = new FileReader();
reader.onload = function(event){
var fileContent = event.target.result;
// Update the output to include the <img> tag with the data URL as the source
document.getElementById("img").src = fileContent;
};
// Produce a data URL (base64 encoded string of the data in the file)
// We are retrieving the first file from the FileList object
reader.readAsDataURL(input.files[0]);
推荐阅读
- javascript - 对于一堆带有 JS 或 Python 的 XHTML 文件,自动加载 XHTML 并单击一个按钮的最简单方法是什么?
- sql - 删除超过 1 个月的记录
- excel - 如何在excel中存储以“+”开头的文本?
- python - 当服务器返回这样的数据时,我该如何抓取?
- javascript - 当应用程序处于后台并且屏幕被锁定时,React-native-background 计时器在 15 分钟后停止工作
- python - 如何循环一个进程
- sql - C#获取gps数据sql到GPRMC插入
- c# - 如何将输入字符串转换为数据表
- jms - 对多个目的地/主题使用相同的 JMS ClientID
- ruby - 如何为 raise、rescue 块编写 rspec