【问题标题】:Determining unknown content-types with the Html5 file api使用 Html5 文件 api 确定未知的内容类型
【发布时间】:2012-06-25 02:50:57
【问题描述】:

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

对于 .doc 文件,我曾预计类型为 "application/msword" 并且按照相同思路 .docx 为 "application/vnd.openxmlformats-officedocument.wordprocessingml.document"

在无法确定类型的情况下,查看文件扩展名并将其与“预期”内容/mime 类型相匹配是正确的做法吗?

示例脚本:

<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>

【问题讨论】:

    标签: javascript html file-upload


    【解决方案1】:

    根据W3 ​​File Api Draft type属性:

    小写的 ASCII 编码字符串,表示媒体类型 Blob,表示为 RFC2046 MIME 类型 [RFC2046]。得到时, 符合标准的用户代理应该返回 Blob 的 MIME 类型,如果它 是已知的。如果符合标准的用户代理无法确定 Blob,它们必须返回空字符串。字符串是有效的 MIME 如果它与第 3.7 节“媒体”中定义的媒体类型令牌匹配,则键入 RFC 2616 的“类型”

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

    是的,你应该这样做:

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

    【讨论】:

    【解决方案2】:

    同意接受的答案,但作为一个仅供参考的人(比如我),他们被困在为什么某些特定文件类型会发生这种情况而不是其他人,请注意,在 Windows 中,如果 mime 不是在用户注册表中适当设置,您将获得空字符串。那几个小时很有趣:)

    【讨论】:

      猜你喜欢
      • 2014-11-17
      • 2014-04-30
      • 2011-01-28
      • 2015-05-22
      • 2022-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多