0

我使用 javascript 对字段图像上传进行表单验证,但我坚持如何通过 javascipt 验证图像大小(宽度和高度),仅通过表单发布 600 宽度和 300 高度的图像。

因此,如果您有任何解决方案,请与 mi 共享演示代码。

提前致谢 !

4

1 回答 1

0

您可以在 javascript 端使用以下函数来检查图像是否具有所需的分辨率。

如果它具有正确的分辨率而不是继续提交表单,否则显示错误消息。

var _URL = window.URL || window.webkitURL;

$("#file").change(function(e) {
    var file, img;

    if ((file = this.files[0])) {
        img = new Image(); // create a Image object which contain height & width property of image
        img.onload = function() {
            alert(this.width + " " + this.height);
        };
        img.onerror = function() {
            alert( "not a valid file: " + file.type);
        };
        img.src = _URL.createObjectURL(file);
    }
});
于 2013-02-23T18:50:56.237 回答