我正在構建圖像調整大小/作物,並且我想在模型(引導程序)中編輯它之後顯示實時預覽。這個應該工作,我相信,但我只是在console.log中得到0。這就需要餵養的寬度和原始圖像的高度到另一個腳本(我將在以後做什麼,只需要他們的console.log /現在一個變量)使用filereader獲取圖像的寬度和高度
function doProfilePictureChangeEdit(e) {
var files = document.getElementById('fileupload').files[0];
var reader = new FileReader();
reader.onload = (function(theFile) {
document.getElementById('imgresizepreview').src = theFile.target.result;
document.getElementById('profilepicturepreview').src = theFile.target.result;
}
);
reader.readAsDataURL(files);
var imagepreview = document.getElementById('imgresizepreview');
console.log(imagepreview.offsetWidth);
$('img#imgresizepreview').imgAreaSelect({
handles: true,
enable: true,
aspectRatio: "1:1",
onSelectEnd: preview
});
$('#resizeprofilepicturemodal').modal('show');
};
大的方式,非常感謝。我誤以爲這些文件會加載readAsDataURL調用。 – 2013-03-19 04:34:02
當文件系統完成從硬盤讀取文件時調用'reader.onload'。本質上,圖像對象已經在瀏覽器中緩衝了圖像數據時調用'image.onload'。我看你怎麼會誤解onload函數;很高興有幫助。 – 2013-03-19 05:45:44
注意:這僅適用於使用「reader.readAsDataURL」的情況。用「reader.readAsBinaryString」你必須採取不同的方式。 – 2014-01-30 14:25:05