将MKV文件添加到文件输入中时,为什么没有返回文件类型? [英] Why is no file type returned when adding an MKV file to a file input?

查看:87
本文介绍了将MKV文件添加到文件输入中时,为什么没有返回文件类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文件添加到基本HTML文件输入中时获取文件的文件/MIME类型.

I am attempting to grab the file/mime type of a file when it is being added to a basic HTML file input.

我的基本HTML:

<form method="post" enctype="multipart/form-data">
 <div>
   <label for="file">Upload an MKV file:</label>
   <input type="file" id="file" name="file" multiple>
 </div>
</form>

我的Javascript:

My Javascript:

var control = document.getElementById("file");
control.addEventListener("change", function(event) {
    var files = control.files;
    for (var i = 0; i < files.length; i++) {
        console.log("Filename: " + files[i].name);
        console.log("Type: " + files[i].type);
    }
}, false);

工作示例

Working Example

例如,添加MP4时,以下内容将输出到控制台:

When adding a MP4 for example, the following is output to the console:

Filename: myvideo.mp4
Type: video/mp4

尽管在添加MKV文件时,控制台会输出该类型为空,如下所示:

Although when adding a MKV file, the console outputs that the type is empty, like so:

Filename: myvideo.mkv
Type: 


经过进一步研究,我发现MKV文件的正式模仿是 video/x-matroska .因此,我尝试将 accept ="video/*" 添加到文件输入中.这样就可以将所有视频类型添加到输入的 MKV文件中,这很奇怪.


After further research, I found that the official mimetype of an MKV file is video/x-matroska. So I tried adding accept="video/*" to the file input. This allows all video types to be added to the input besides MKV files, which is very odd.

那为什么我的文件输入无法检测到MKV文件的文件类型?

So why is my file input unable to detect the file type of MKV files?

推荐答案

"MKV文件的官方模仿是video/x-matroska"

不幸的是,这不是事实. IANA 仍未在其列表中认可它官方MIME类型.

This is unfortunately not true. IANA still hasn't endorsed it in its list of official MIME types.

现在,在某些浏览器+操作系统配置中,您实际上可能会拥有它,因为 type 检查是由浏览器非常宽松地进行的,通常只检查扩展名和已知映射MIME扩展.某些浏览器本身没有此类列表,因此将使用您的用户可以控制的操作系统.

Now, it is possible that in some browsers + OS configuration you would actually have it, because the type checking is made quite leniently by browsers, generally checking against only the extension and a map of known extensions to MIME. Some browsers don't have such list themselves, and thus will use the OS one, which your user may control.

但是在我的情况下(macOs),除了空字符串外,没有其他浏览器会显示任何内容...

But in my case (macOs) no browser seems to show anything else than the empty string...

Firefox实际上有一个未解决的问题.

但是,您可以通过在 accept 属性中明确设置扩展名来解决此问题:

However, you can workaround that issue by setting explicitly the extension in your accept attribute:

<input type="file" accept="video/*,.mkv">

这篇关于将MKV文件添加到文件输入中时,为什么没有返回文件类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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