【发布时间】:2020-05-16 21:35:28
【问题描述】:
我正在使用一个函数来验证上传图像的大小。效果很好,现在我还想检查图像是否比某些像素(如 1280 像素)宽。但是 var 宽度仍然为零。我已经尝试过使用outerwith、innerwidth和file.files[0].width;
function ValidateSize(file) {
var FileSize = file.files[0].size / 1024 / 1024; // in MB
var NewFilze = FileSize.toFixed(2);
var height = file.height;
var width = file.width;
if (FileSize > 13) {
alert("The file size of your picture (" + NewFilze + " MB) exceeds the maximum allowed filesize (12,5 MB). Still problems with resizing your pictures? Use for example www.imageresize.org.");
}else if(width < 1280){
alert("The width of your image (" + width + " pixels) is lower than the required minimum width (1280 pixels)");
}else{
}
}
提前致谢
【问题讨论】:
-
此时文件对象是什么样子的。它有 .width 和 .height 吗?
-
对于尺寸,您从
file.files[0]获取数据,但对于高度和宽度,您只需检查file? 是file和file.files应该是什么 - 有什么区别? -
因为它可以检测文件大小,我假设它可以检测到 with 和 height。
-
也许也许不是。文件对象可能只是一个文件。您可能需要考虑先将文件转换为图像。
-
看看使用 FileReader 然后转换成 Image。这个问题非常彻底地涵盖了它:stackoverflow.com/questions/7460272/…
标签: javascript