我想驗證圖像大小和擴展名,然後再上傳圖像。我有圖像擴展的代碼,我想限制圖像的大小。 這裏是圖像擴展代碼:如何在上傳前驗證圖片大小?
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()">
我想在上面的代碼中添加圖像尺寸驗證 – stark 2014-12-05 06:17:13
可能重複[如何預覽圖片,上傳前獲取文件大小,圖像高度和寬度?](http://stackoverflow.com/問題/ 12570834 /如何預覽圖像獲取文件大小圖像高度和寬度之前上傳) – 2014-12-05 06:19:05