FileUpload:当文件类型是图像时显示警报

FileUpload: Show alert when file type is an image

一个简单的文件上传

<dl>
    <dt><label for="fileupload">{L_FILENAME}:</label></dt>
    <dd>
        <input type="file" name="fileupload" id="fileupload" maxlength="{FILESIZE}" 
               value="" />
    </dd>
</dl>

我需要检查图像 (jpg, jpeg, gif, png)。当有图像时,警告消息应显示:"You are trying to upload an image. Please use the image uploader".

注意:图像检查应在选择文件后立即开始,而不是在提交表单后开始。

jQuery可以吗?

我尝试了很多解决方案都没有成功。谢谢!

如果我是对的,那么您正在寻找 .change 活动。

$("#fileupload").change(function (e) {});

现在使用下面的代码,

$("#fileupload").change(function(e) {
    var val = $(this).val();
    if (val.match(/(?:gif|jpg|png|bmp)$/)) {
        alert("You are trying to upload an image. Please use the image uploader!");
    }
});

Demo

也许,您可以使用 Regex 来匹配您的文件扩展名 /(?:gif|jpg|JPG|JPEG|png|PNG|bmp)$/

Updated Fiddle

希望对您有所帮助!