JS 和 type.match 作为文件 mime 类型 - 需要建议
今天我遇到了一件有趣的事情,即 FF File API 和按类型分离文件。好的,这是一个小片段,因为
if (!input.files[0].type.match('image.*')) {
window.alert("Select image please");
return;
}
它控制只读图像。但是例如 doc 文件和 pdf 呢?我找不到这件事的有用示例,因此我希望您可以分享一些片段。我感兴趣的是检测不同的文件类型,但是如何使用 JS 及其 type.match 绑定控制不同的文件类型?
这里是基本代码
任何有用的评论表示赞赏:)
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 :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因此,基本思想是此代码使用文件对象,有关它们的更多信息,请参阅:
按照 w3,File对象的
type
属性是MIME类型。这是在 RFC 2046 中定义的。但规范本身并不是最有趣的部分,更有趣的是现有 MIME 类型的列表这里 或此处最常用的一个。在此代码中,他们使用
type
属性并对其执行正则表达式(请参阅匹配和RegExp了解更多信息)。他们的正则表达式表示,如果类型包含image
就可以。要制作您自己的选择器,您必须结合以上内容。 (有些示例使用 === 而不是 match,因为 mime 类型是整个类型)
例如,可以进行以下检查:
input.files[0].type==='application/pdf'
input.files[0].type.match('video.*')
并且很快。
之后,如果您只想匹配某些扩展名,您可以在文件的
name
属性上使用选择器(例如,要检查不同类型的文档,您可以查看它是否是 .pdf、 .odt ...),例如使用input.files[0].name.match('\.pdf')
。但恕我直言,不建议这样做,因为用户可以轻松地使用它(删除或更改它们)。
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 containsimage
.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:
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
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 exampleinput.files[0].name.match('\.pdf')
.But imho that's not advised, as the user could easily play with that (removing or changing them).
这可能会有所帮助:
这会检查所有可接受的 mime 类型
This might help:
This checks for all acceptable mime types
这与
*/*
匹配。/
前后至少需要一个字符/(.+)\/.+/
结果:
image/jpeg
imagejpeg应用程序图像/jpg/jpegapplication/x-abiword
application/vnd.amazon.ebook
application/x-bzip2
application/vnd.openxmlformats-
officedocument.wordprocessingml.document
application/epub+zip
application/vnd.apple.installer+xml
This matches
*/*
. It requires at least one character before and after/
/(.+)\/.+/
Results:
image/jpeg
imagejpegapplicationimage/jpg/jpegapplication/x-abiword
application/vnd.amazon.ebook
application/x-bzip2
application/vnd.openxmlformats-
officedocument.wordprocessingml.document
application/epub+zip
application/vnd.apple.installer+xml