首页 > 解决方案 > 为什么 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]);

标签: javascript

解决方案


更新后的值由事件属性提供给 onloadevent.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]);

推荐阅读