JS 和 type.match 作为文件 MIME 类型 - 需要建议 [英] JS and type.match as file mime type - need advice

查看:21
本文介绍了JS 和 type.match 作为文件 MIME 类型 - 需要建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

if (!input.files[0].type.match('image.*')) {window.alert("请选择图片");返回;}

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

这里是基本代码

感谢任何有用的评论:)

解决方案

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

根据 w3 的规定,文件的 type 属性object 是 MIME 类型.这在 RFC 2046 中定义.但是规范本身并不是最有趣的部分,更有趣的是现有 MIME 类型的列表 here 或最常用的此处.

在此代码中,他们使用 type 属性并对其执行正则表达式(请参阅 匹配RegExp 以获取更多信息).他们的正则表达式说如果类型包含 image 就可以了.

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

  • 文档(不仅仅是 pdf,例如 odt 也有这种类型):input.files[0].type==='application/pdf'
  • 音频:input.files[0].type.match('audio.*')
  • 视频:input.files[0].type.match('video.*')

等等.

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

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

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?

Here is the base code

Any useful comment is appreciated :)

解决方案

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

As specified per w3, the type attribute 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 here or the most used one here.

In this code, they use the type attribute and execute a regexp on it (see match and RegExp for more info). Their regexp says that it's ok if the type contains 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:

  • 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.*')

And so on.

After that you can use a selector on the name attribute 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).

这篇关于JS 和 type.match 作为文件 MIME 类型 - 需要建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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