3
A
回答
2
如果您正在检查的文件大小,你可以使用类似uploadify上传前检查文件的大小。
检查实际的文件尺寸(高度/宽度)可能需要在服务器上完成。
1
2
我已经在chrome中测试过这段代码,它工作正常。
var x = document.getElementById('APP_LOGO'); // get the file input element in your form
var f = x.files.item(0); // get only the first file from the list of files
var filesize = f.size;
alert("the size of the image is : "+filesize+" bytes");
var i = new Image();
var reader = new FileReader();
reader.readAsDataURL(f);
i.src=reader.result;
var imageWidth = i.width;
var imageHeight = i.height;
alert("the width of the image is : "+imageWidth);
alert("the height of the image is : "+imageHeight);
0
像这样的东西应该有或没有“多”异步加载的形式,投入应对设置,避免在比赛条件下使用FileReader.readAsDataURL和Image.src当这种情况发生。
$('#formContainer').on('change', '#inputFileUpload', function(event) {
var file, _fn, _i, _len, _ref;
_ref = event.target.files;
_fn = function(file) {
var reader = new FileReader();
reader.onload = (function(f) {
return function() {
var i = new Image();
i.onload = (function(e) {
var height, width;
width = e.target.width;
height = e.target.height;
return doSomethingWith(width, height);
});
return i.src = reader.result;
};
})(file);
return reader.readAsDataURL(file);
};
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
file = _ref[_i];
_fn(file);
}
});
注:jQuery的需要,HTML5,钩子一样的结构:
<div id="formContainer">
<form ...>
...
<input type="file" id="inputFileUpload"></input>
...
</form>
</div>
0
你需要检查图像的宽度和高度时,图像的onload。
var img = new Image;
img.src = URL.createObjectURL(file);
img.onload = function() {
var picWidth = this.width;
var picHeight = this.height;
if (Number(picWidth) > maxwidth || Number(picHeight) > maxheight) {
alert("Maximum dimension of the image to be 1024px by 768px");
return false;
}
}
相关问题
- 1. 检查图像大小之前上传
- 2. 上传前调整图片的大小
- 3. Asp.Net上传前检查文件大小
- 4. 在将图片上传到Firebase之前调整图片大小
- 5. 如何在上传之前检查图像大小(例如1MB)
- 6. 上传后调整图片大小
- 7. 上传时调整图片的大小
- 8. 图片大小和php上传
- 9. 如何在上传前验证图片大小?
- 10. 在上传HTML文件之前调整图片的大小5
- 11. 如何使用ajaxupload3.5上传照片时检查文件大小
- 12. 如何检查上传文件大小
- 13. PHP - 检查文件上传大小
- 14. Java文件上传大小检查
- 15. jquery文件上传大小检查
- 16. 检查文件大小上传
- 17. 在Internet Explorer中上传之前检查文件大小的Javascript
- 18. 在上传之前检查文件大小的其他方法
- 19. 如何在uploadify中上传图片前检查图片的宽度和高度?
- 20. 上传图片然后调整图片大小
- 21. Cakephp 2+上传图片时调整图片大小和裁剪
- 22. jQuery文件上传 - 获取上传图片的大小
- 23. 上传前检查图像尺寸
- 24. 检查大小,宽度和高度后上传图像
- 25. Drupal6:大量图片上传?
- 26. Laravel上传大量图片
- 27. swift ios在上传之前缩小图像大小
- 28. 3g上合理的图片上传文件大小
- 29. php图片调整大小不能在图像上传
- 30. 如何在C++中检查最大文件大小上传?