我正在使用
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?
解决方法
铬跟踪器的解释:
This is not a bug. WebKit is just more strict. You must instantiate a new Image() object before the replacement,like this:
var photo = document.getElementById('image_id'); var img = new Image(); img.addEventListener('load',myFunction,false); img.src = 'http://newimgsource.jpg'; photo.src = img.src;
来源:http://code.google.com/p/chromium/issues/detail?id=7731#c12