2
이 코드와 클라이언트 브라우저에서 일부 이미지를로드 traing 오전 :을 FileReader + 캔버스 이미지 로딩 문제
function addFiles()
var input = document.querySelector("input[type='file']");
var files = input.files;
var previewEl = document.getElementById("preview");
for (var i = 0; i < files.length; i++) {
if (!files[i].type.match(/image.*/)) {
alert("This is not image!");
continue;
};
var divEnvelop = document.createElement('div');
divEnvelop.setAttribute('class','imgPrevEnvelop');
divEnvelop.setAttribute('id',"img"+i);
previewEl.appendChild(divEnvelop);
var img = document.createElement("img");
img.file = files[i];
var reader = new FileReader();
reader.onload = (function(aImg, aName, aEnvelop) { return function(e) {
aImg.src = e.target.result;
//here I don't have img.width - problem
createImgCanvas(aImg, aImg.width, aImg.height, 300, 300, aName, aEnvelop);
}; })(img,files[i].name, divEnvelop);
reader.readAsDataURL(files[i]);
}
}
function createImgCanvas(img, imgWidth, imgHeight, width, height, label, divEnvelop) {
if (imgWidth > imgHeight) {
if (imgWidth > width) {
imgHeight *= width/imgWidth;
imgWidth = width;
}
} else {
if (imgHeight > height) {
imgWidth *= height/imgHeight;
imgHeight = height;
}
}
var canvas = document.createElement('canvas');
canvas.setAttribute('id',label);
canvas.width = imgWidth;
canvas.height = imgHeight;
var imageCanvas2D = canvas.getContext("2d");
try{
imageCanvas2D.drawImage(img, 0, 0, imgWidth, imgHeight);
} catch(err) { alert(err);}
divEnvelop.appendChild(canvas);
}
하지만 문제는 내가 img.width 재산이없는 wher, reader.onload 기능에있다 . 여전히 0으로 설정됩니다. 이 동작은 크롬에서도 발생하므로 아마도 내 잘못 일 수 있습니다. 나 한테 말할 수 있니, 문제가 어디 있니? 나는 아직을 FileReader 객체에 근무 한 적이없는
빠른 답변을 해 주셔서 감사합니다. 그게 다야! 그것은 이제 완벽하게 일하고 있습니다. –