How to check file MIME type with javascript before upload?

前端 未结 9 2099
挽巷
挽巷 2020-11-22 03:50

I have read this and this questions which seems to suggest that the file MIME type could be checked using javascript on client side. Now, I understand that the real validati

9条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 04:43

    As Drake states this could be done with FileReader. However, what I present here is a functional version. Take in consideration that the big problem with doing this with JavaScript is to reset the input file. Well, this restricts to only JPG (for other formats you will have to change the mime type and the magic number):

    Take in consideration that this was tested on latest versions of Firefox and Chrome, and on IExplore 10.

    For a complete list of mime types see Wikipedia.

    For a complete list of magic number see Wikipedia.

提交回复
热议问题