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

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

问题描述

今天,我面对一个有趣的事情,作为FF文件API和单独的文件的类型。确定这是一个小片段作为

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

它控制只读的图像。但是,doc文件和pdf例如呢?我找不到有用的例子,所以我希望你能分享一些片段。我感兴趣的东西在检测不同的文件类型,但我怎么能控制不同的文件类型与JS和它的type.match绑定?



这里是基本代码



<所以,基本的想法是,这个代码使用文件对象,以获取更多的信息他们看到:





根据 W3 指定,类型属性File对象是一个MIME类型。这是在 RFC 2046 中定义的。但规范本身不是最有趣的部分,更有趣的是现有的MIME类型列表 或使用最多的此处



<在这段代码中,他们使用类型属性并对其执行正则表达式(参见匹配 RegExp 了解更多信息)。他们的正则表达式表示,如果类型包含 image

就可以了。要创建自己的选择器,结合以上。 (一些示例使用===而不是匹配,因为MIME类型是整个类型)
例如,可以进行以下检查:


  • 文档(不仅仅是pdf,odt也有这个类型): input.files [0] .type ==='application / pdf'

  • audio: input.files [0] .type.match('audio。*')

  • video: input.files [0] .type.match('video。*')



等等。

之后,您可以在名称文件的属性,如果您希望只匹配某个扩展名(例如,在不同类型的文档之间进行检查,您可以查看它是否为.pdf,.odt ...),例如输入.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天全站免登陆