图像,onload 事件在 Chrome 中不起作用
我正在使用 html5 创建拖放图像上传功能。这在 Firefox 中非常适合我,但在 chrome 中,图像 onload 事件仅在第一次触发。如果我只在第一个作品中拖动多个图像,而如果我在其中拖动第二个图像,则会失败。我相信问题出在图像加载上。
这是我的代码的工作方式,我删除了不相关的部分:
var img = document.createElement("img");
var reader = new FileReader();
var canvas = document.createElement("canvas");
var canvasData;
var ctx = canvas.getContext("2d");
var myFiles;
var i = 0;
reader.onload = (function (aImg)
{
return function (e)
{
aImg.src = e.target.result;
};
})(img);
img.onload = function (){
//resizes image
//draws it to the canvas
//posts to server
i++;
if(i < myFiles.length){
processNext(i);
}
}
function processNext(filei) {
var file = myFiles[filei];
img.file = file;
reader.readAsDataURL(file);
}
i = 0;
myFiles = files;
processNext(0);
有谁知道为什么这在 Firefox 中有效,但在 chrome 中无效?
I'm using html5 to create drag and drop image upload functionality. This works great for me in firefox but in chrome the image onload event only fires the first time. If I drag multiple images in only the first works and if I drag a second in it fails. I believe the problem is with the image onload.
here is the way my code works I have removed the irrelevant sections:
var img = document.createElement("img");
var reader = new FileReader();
var canvas = document.createElement("canvas");
var canvasData;
var ctx = canvas.getContext("2d");
var myFiles;
var i = 0;
reader.onload = (function (aImg)
{
return function (e)
{
aImg.src = e.target.result;
};
})(img);
img.onload = function (){
//resizes image
//draws it to the canvas
//posts to server
i++;
if(i < myFiles.length){
processNext(i);
}
}
function processNext(filei) {
var file = myFiles[filei];
img.file = file;
reader.readAsDataURL(file);
}
i = 0;
myFiles = files;
processNext(0);
Does anyone know why this works in firefox but not chrome?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
铬追踪器的解释:
source: http://code.google.com/p/chromium/issues/detail?id=7731#c12
Explanation from chromium tracker:
source: http://code.google.com/p/chromium/issues/detail?id=7731#c12
这很奇怪,以上都不适合我。我将图像变量定义为本地变量并将其更改为全局变量,然后它开始工作。这有道理吗?有人可以解释一下吗?
这对我不起作用:
这确实有效:
This is strange, none of the above worked for me. I was defining the image variable as local and change it to global and it started working. Does this make sense? Can somebody explain it?
This didnt worked for me:
This did work: