使用Html5文件API确定未知的内容类型 [英] Determining unknown content-types with the Html5 file api

查看:194
本文介绍了使用Html5文件API确定未知的内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过一个小的文件上传脚本(学习经验),我注意到,当选择与Microsoft Office相关的文件( .doc .docx )文件对象没有指定的类型:

application / msword 并沿着相同的思路.docx是application / vnd.openxmlformats-officedocument.wordprocessingml.document



在无法确定类型的情况下,查看文件扩展名并将其与预期的内容/ MIME类型相匹配的正确操作? b
$ b

示例脚本:

 < div id =fileUpload> 
< input type =fileid =fileElemstyle =display:none;平变化= handleFiles(this.files)/>
< a href =#id =fileSelect>选择一些文件< / a>
< / div>

< script type =text / javascript>
var fileSelect = document.getElementById(fileSelect),
fileElem = document.getElementById(fileElem);

fileSelect.addEventListener(click,function(e){

if(fileElem){
fileElem.click();
}

e.preventDefault();
},false);

函数handleFiles(文件){
console.log(files);
}

< / script>


解决方案

根据W3 ​​File Api Draft的类型attributte:
$ b


以小写字母ASCII编码的字符串表示
的媒体类型Blob,表示为RFC2046 MIME类型[RFC2046 ]。获取时,
符合的用户代理应该返回Blob的MIME类型,如果
已知。如果符合的用户代理不能确定
的媒体类型的Blob,他们必须返回空字符串。如果字符串与RFC 2616的第3.7节媒体
类型中定义的媒体类型标记匹配,则该字符串是有效的MIME
类型

所以基本上,如果它不是HTTP / 1.1的有效或媒体类型,它将是空的。
无论如何。

是的,你应该这样做:

  if(type ===){
//获取扩展名并匹配MIME类型列表。 (http://www.htmlquick.com/es/reference/mime-types.html)
}


I'm working through a small file upload script (learning experience) and I noticed that when selecting microsoft office related files (.doc or .docx for example) the file objects do not have a type specified:

For .doc files I had expected the type to be "application/msword" and along the same train of thought .docx to be "application/vnd.openxmlformats-officedocument.wordprocessingml.document".

In the cases when the type cannot be determined is the correct course of action to look at the file extension and match that to the "expected" content / mime type?

Sample script:

<div id="fileUpload">
    <input type="file" id="fileElem" style="display:none;" onchange="handleFiles(this.files)"/>
    <a href="#" id="fileSelect">Select some files</a>
</div>

<script type="text/javascript">
    var fileSelect = document.getElementById("fileSelect"),
        fileElem = document.getElementById("fileElem");

    fileSelect.addEventListener("click", function (e) {

        if (fileElem) {
            fileElem.click();
        }

        e.preventDefault();
    }, false);

    function handleFiles(files) {
        console.log(files);
    }

</script>

解决方案

According to the W3 File Api Draft the type attributte:

The ASCII-encoded string in lower case representing the media type of the Blob, expressed as an RFC2046 MIME type [RFC2046]. On getting, conforming user agents SHOULD return the MIME type of the Blob, if it is known. If conforming user agents cannot determine the media type of the Blob, they MUST return the empty string. A string is a valid MIME type if it matches the media-type token defined in section 3.7 "Media Types" of RFC 2616

So basically, if it's not a valid or media-type of HTTP/1.1 it will be empty. Anyways.

Yeah, you should do something like:

if(type === "") {
  //Get extension and match to a MIME-types list. (http://www.htmlquick.com/es/reference/mime-types.html)
}

这篇关于使用Html5文件API确定未知的内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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