jQuery或JavaScript:获取客户端文件的MIME类型 [英] jQuery or JavaScript: get MIME type of client file

查看:1178
本文介绍了jQuery或JavaScript:获取客户端文件的MIME类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用jQuery或JavaScript在我的应用程序的客户端检测文件的MIME类型。有没有办法做到这一点?
谢谢。

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.

推荐答案

您可以使用AJAX,发出HEAD请求,并检查<$的响应头c $ c>内容类型标题。但是只有在从HTTP服务器获取文件时才有效。

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.

获取文件的MIME类型使用HTML文件选择器选择,不提交任何内容,请尝试:

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

示例

http://jsbin.com/akati3/2

尝试选择图片,检查MIME类型,然后尝试提交。然后尝试一些不是图像的东西。

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

这篇关于jQuery或JavaScript:获取客户端文件的MIME类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆