jQuery or [removed] get MIME type of client file

后端 未结 3 2235
孤城傲影
孤城傲影 2020-12-05 07:40

I would like to detect the MIME type of a file on the client side of my application using jQuery or JavaScript. Is there a way to do this? Thanks.

3条回答
  •  死守一世寂寞
    2020-12-05 08:07

    You could use AJAX, make a HEAD request, and inspect the response headers for the Content-type header. But that only works if you're getting a file from a HTTP server.


    To get the MIME type of a file chosen with an HTML file chooser, without submitting anything, try:

    document.getElementById('fileChooserID').files[0].type // e.g. image/png
    

    Example

    http://jsbin.com/akati3/2

    Try choosing an image, check the MIME type, and try to submit it. Then try something else that's not an image.

提交回复
热议问题