开发者

JavaScript validation for invalid file type

开发者 https://www.devze.com 2022-12-11 17:36 出处:网络
I have a scenario wherein user is trying to view an invalid image file (for example an .dll file renamed to JPG extenstion). How do I alert the user using JavaScript tha开发者_如何学运维t this is an i

I have a scenario wherein user is trying to view an invalid image file (for example an .dll file renamed to JPG extenstion). How do I alert the user using JavaScript tha开发者_如何学运维t this is an invalid file?


JavaScript, running in the browser in a standard security context, has no way to determine this.

You have to handle this check server side.


There is no way doing it using JavaScript.


There is a way of doing it with javascript.

If the browser supports window.FileReader and window.Blob, which most do, then yes, you can.

if ( window.FileReader && window.Blob )
{
    // get your file from wherever...
    // tacky check jpg in the mime type 
    if ( yourFile.type.indexOf("jpeg") )
    {
        ....
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消