我有〈输入类型=文件和一个按钮,以更新该文件的服务器。我想在其中一个按钮,它将验证上传的文件分辨率(宽度和高度)点击功能
0g0grzrc1#
function validateImageFile(fileInput) { const file = fileInput.files[0]; const reader = new FileReader(); reader.onload = function(event) { const img = new Image(); img.onload = function() { const width = this.width; const height = this.height; if (width === 0 || height === 0) { alert("Invalid image file. Please select a valid image."); fileInput.value = ""; // added a condtion , you chnage it according to your need } else if (width < 600 || height < 400) { alert("Image resolution is low. Please select an image with at least 600 x 400 resolution."); fileInput.value = ""; } else { // Image is valid. } }; img.src = event.target.result; }; reader.readAsDataURL(file); }
1条答案
按热度按时间0g0grzrc1#