jQuery 或 JavaScript:获取客户端文件的 MIME 类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4581308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 13:04:20  来源:igfitidea点击:

jQuery or JavaScript: get MIME type of client file

javascriptjquerymime-types

提问by SZH

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.

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

回答by Delan Azabani

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

您可以使用 AJAX,发出 HEAD 请求,并检查标头的响应标Content-type头。但这只有在您从 HTTP 服务器获取文件时才有效。



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

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

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

Example

例子

http://jsbin.com/akati3/2

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.

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

回答by karim79

The only way to reliablydetect a mime type is to parse the file on the server side, to confirm that it is the type that the user claims it to be, or that it fits a list of allowed types. Things to consider:

可靠地检测 MIME 类型的唯一方法是在服务器端解析文件,确认它是用户声称的类型,或者它符合允许的类型列表。需要考虑的事项:

1 - JavaScript has limited access to the local filesystem, and with good reason.

1 - JavaScript 对本地文件系统的访问受到限制,这是有充分理由的。

2 - You cannot trust a mime type which has been received from a browser. It will not necessarily match the mime type of the sent file.

2 - 您不能信任从浏览器收到的 MIME 类型。它不一定与发送文件的 MIME 类型匹配。

3 - In a situation where the user is allowed to upload files which fit a 'whitelist' of allowed types, validation is probably necessary anyway - considering that the application might have to actually do something with the file beyond storing them, which would at the very least involve parsing their headers for information such as run length (for a video) version number (for a Word document) and so on.

3 - 在允许用户上传符合允许类型的“白名单”的文件的情况下,无论如何验证可能是必要的 - 考虑到应用程序可能必须实际对文件执行某些操作而不是存储它们,这将在至少涉及解析它们的标题以获取诸如运行长度(对于视频)版本号(对于 Word 文档)等信息。

回答by christian

the idea IS NOT trust in the Browser..the idea is perform this validations on SERVER SIDE but, what a usefull thing if prior to send a 20MB file to the browser and next get rejected because a rule in the server...so, it is a good idea to "pre-check" if this file "is a candidate" to be uploaded, the final validation will be performed in the server.

这个想法不信任浏览器......这个想法是在服务器端执行这个验证但是,如果在将 20MB 文件发送到浏览器之前然后因为服务器中的规则而被拒绝,这是多么有用的事情......所以,如果此文件“是候选”要上传,则“预先检查”是个好主意,最终验证将在服务器中执行。