我正在使用一个函数来验证上传图像的大小。效果很好,现在我还想检查图像是否比某些像素(例如1280像素)宽。但是var宽度仍为零。我已经尝试使用externalwith,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{
}
}
提前致谢
如果无法自己进行测试,建议您尝试以下操作:
function ValidateSize(file) {
var FileSize = file.files[0].size / 1024 / 1024; // in MB
var NewFilze = FileSize.toFixed(2);
var reader = new FileReader();
reader.readAsDataURL(fileUpload.files[0]);
reader.onload = function (e) {
var image = new Image();
image.src = e.target.result;
image.onload = function () {
var height = this.height;
var width = this.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{
}
}
我应该注意,这未经测试,并且取决于您的js在某些浏览器(最好是chrome或FF)上运行。如果是服务器端,我认为visibleman被链接为最佳答案。
祝你好运,黑客愉快!
归功于我应该获得图像代码的适当位置
是的,不知道为什么我只是以为它是服务器端的。
该功能看起来应该在服务器端,但我更像是前端人员,所以我想为什么不尝试前端解决方案。