Javascript JS 和 type.match 作为文件 MIME 类型 - 需要建议

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7395548/
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-24 02:07:27  来源:igfitidea点击:

JS and type.match as file mime type - need advice

javascriptfirefoxgoogle-chromefile-iofileapi

提问by user592704

Today I faced an interesting thing as FF File API and separate files by their types. OK here is a little snippet as

今天我遇到了一个有趣的事情,比如 FF File API 和按类型分离文件。好的,这里有一个小片段

if (!input.files[0].type.match('image.*')) {
            window.alert("Select image please");
            return;
        }

it controls image being read only. But what about doc files and pdf for example? I couldn't find useful examples for this thing so I hope you can share some snippets. The thing I am interested in detecting different file types but how can I control different file types with JS and its type.match bound?

它控制图像是只读的。但是例如 doc 文件和 pdf 呢?我找不到这件事的有用例子,所以我希望你能分享一些片段。我对检测不同的文件类型感兴趣,但如何使用 JS 及其 type.match 绑定来控制不同的文件类型?

Hereis the base code

是基本代码

Any useful comment is appreciated :)

任何有用的评论表示赞赏:)

回答by Py.

So the base idea is that this code uses File Objects, for more info on them see :

所以基本思想是这段代码使用文件对象,有关它们的更多信息,请参见:

As specified per w3, the typeattribute of the File object is a MIME type. This is defined in the RFC 2046. But the spec itself isn't the most interesting part, what's more interesting is the list of the existing MIME type hereor the most used one here.

根据w3 的规定,typeFile 对象的属性是 MIME 类型。这在RFC 2046 中定义。但规范本身并不是最有趣的部分,什么是更有趣的是现有的MIME类型的列表在这里还是最常用的一个位置

In this code, they use the typeattribute and execute a regexp on it (see matchand RegExpfor more info). Their regexp says that it's ok if the type contains image.

在此代码中,他们使用该type属性并对其执行正则表达式(有关更多信息,请参阅matchRegExp)。他们的正则表达式说如果类型包含image.

To make your own selector, you'll have to combine the above. (some of the example use === instead of match because the mime type is the entire type) For example the following check are possible:

要制作自己的选择器,您必须结合上述内容。(某些示例使用 === 而不是 match 因为 mime 类型是整个类型)例如,以下检查是可能的:

  • document (not pdf only, odt have this type as well for example) : input.files[0].type==='application/pdf'
  • audio : input.files[0].type.match('audio.*')
  • video : input.files[0].type.match('video.*')
  • 文档(不仅仅是 pdf,例如 odt 也有这种类型): input.files[0].type==='application/pdf'
  • 声音的 : input.files[0].type.match('audio.*')
  • 视频 : input.files[0].type.match('video.*')

And so on.

等等。

After that you can use a selector on the nameattribute of the file if you wish to match only certain extension (for example to check between different kind of document, you could look if it's a .pdf, a .odt ...) using for example input.files[0].name.match('\.pdf'). But imho that's not advised, as the user could easily play with that (removing or changing them).

之后,name如果您只想匹配特定的扩展名(例如检查不同类型的文档,您可以查看它是否是 .pdf、.odt ...),您可以在文件的属性上使用选择器,使用 for例子input.files[0].name.match('\.pdf')。但是恕我直言,不建议这样做,因为用户可以轻松地玩弄它(删除或更改它们)。

回答by Shalmali

This might help:

这可能有帮助:

file.mimetype.match('text.*|image.*|application.*')

This checks for all acceptable mime types

这会检查所有可接受的 MIME 类型