我在上传功能期间无法获取图像的宽度
和高度
。
这是我目前的代码:
// internal function that creates an input element
var file=newElement("input",0,"");
// sets the input element as type file
file.type="file";
file.multiple="multiple";
file.name="photoupload[]";
file.accept="image/*";
file.onchange=function(e){
// internal function that creates an image element
var img=newElement("img",0,"");
img.src=URL.createObjectURL(this);
img.onload=function(){
var cw=img.clientWidth;
var ch=img.clientHeight;
alert(cw);alert(ch);
}
}
file.click();
不太确定如何解决这个问题。任何帮助表示赞赏。
最佳答案
将onload调到src改变之前,使用width和height。
改变src之前onload需要存在
我假设 newElement 是一个执行 document.createElement 的函数 - 如果它不返回图像对象,您需要将 onload 移动到该函数
// internal function that creates an input element
var file=newElement("input",0,"");
// sets the input element as type file
file.type="file";
file.multiple="multiple";
file.name="photoupload[]";
file.accept="image/*";
file.onchange=function(e){
// internal function that creates an image element
var img=newElement("img",0,"");
img.onload=function(){
var cw=img.width;
var ch=img.height;
console.log(cw,ch);
}
img.src=URL.createObjectURL(this);
}
file.click();
关于javascript - 在上传javascript期间获取图像的宽度和高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46477895/