gpt4 book ai didi

javascript - 如何在上传前验证图片大小?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:33:15 25 4
gpt4 key购买 nike

我想在上传图片之前验证图片大小和扩展名。我有图片扩展名的代码,我想限制图片的大小。这是图像扩展的代码:

function ValidateFileUpload() {
var fuData = document.getElementById('fileChooser');
var FileUploadPath = fuData.value;


if (FileUploadPath == '') {
alert("Please upload an image");

} else {
var Extension = FileUploadPath.substring(
FileUploadPath.lastIndexOf('.') + 1).toLowerCase();



if (Extension == "gif" || Extension == "png" || Extension == "bmp"
|| Extension == "jpeg" || Extension == "jpg") {


if (fuData.files && fuData.files[0]) {
var reader = new FileReader();

reader.onload = function(e) {
$('#blah').attr('src', e.target.result);
}

reader.readAsDataURL(fuData.files[0]);
}

}


else {
alert("Photo only allows file types of GIF, PNG, JPG, JPEG and BMP. ");

}
}
}

HTML代码

<input type="file" name="image"  id="fileChooser" style="height:28px; width:175px;" onchange="return ValidateFileUpload()">

最佳答案

您可以将此代码与经过测试的 HTML 5 一起使用 演示:Demo for this

<input type="file" id="file" />

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

$("#file").change(function(e) {

var image, file;

if ((file = this.files[0])) {

image = new Image();

image.onload = function() {

alert("The image width is " +this.width + " and image height is " + this.height);
};

image.src = _URL.createObjectURL(file);


}

});

关于javascript - 如何在上传前验证图片大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27309921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com