简体   繁体   English

Chrome for Android上的Html文件输入缺少扩展名和mime类型

[英]Html File Input on Chrome for Android missing extension and mime type

In html/js, when you select a file on Chrome (43) for Android (5.1.1), the resulting files name is missing the extension and the file type is an empty string. 在html / js中,当您在Chrome(43)for Android(5.1.1)上选择文件时,生成的文件名缺少扩展名,文件类型为空字符串。 Wondering if there is way to get the missing information? 想知道是否有办法获取遗失的信息? And if not, is there a reasonable way to do clientside side validation on file types? 如果没有,是否有合理的方法对文件类型进行客户端验证?

Please see the example fiddle below and note that I get extension and mime type on windows desktop browsers and ios safari. 请参阅下面的示例小提琴,并注意我在Windows桌面浏览器和ios safari上获得扩展和mime类型。

http://jsfiddle.net/Lfg3xhy4/ http://jsfiddle.net/Lfg3xhy4/

$(function(){
    $("#fileInput").on("change", function(e){
        $("#name").html(e.target.files[0].name);
        $("#type").html(e.target.files[0].type);
    });
});

Update I noticed when using the android Documents browser, the Audio tab (which I was using to select files) does not seem to supply file extension and mime type. 更新我注意到在使用android Documents浏览器时,Audio选项卡(我用来选择文件)似乎不提供文件扩展名和mime类型。 If I navigate to files using any other tab, everything seems ok. 如果我使用任何其他选项卡导航到文件,一切似乎都可以。

Via the android Documents file browser, some of the tabs do not provide file extension and type. 通过android Documents文件浏览器,某些选项卡不提供文件扩展名和类型。 The Audio tab (which displays audio metadata) and some of the connected storage accounts for example. 例如,“音频”选项卡(显示音频元数据)和一些连接的存储帐户。

As such, another way to do file type validation is to use "file signatures" http://www.filesignatures.net/index.php?page=all 因此,另一种进行文件类型验证的方法是使用“文件签名” http://www.filesignatures.net/index.php?page=all

Essentially you take a look at a portion of the binary file. 基本上你看一下二进制文件的一部分。 Simplified example below. 以下是简化示例。

var audioFile;  //Set from elsewhere
if (FileReader) {
    var reader = new FileReader();
    reader.addEventListener("load", function(e) {
        var mp3FileSignature = [255, 251, 48];
        var id3FileSignature = [73, 68, 51];
        //There are actually quite a few more scenarios to handle for mp3's in particular
        //Most file types are simpler

        var arrayBuffer = e.target.result;

        if (arrayBuffer && arrayBuffer.byteLength >= 3) {
            var slice = arrayBuffer.slice(0, 3);
            var view = new Uint8Array(slice);
            if ((mp3FileSignature[0] != view[0] || mp3FileSignature[1] != view[1] || mp3FileSignature[2] != view[2])  
                && (id3FileSignature[0] != view[0] || id3FileSignature[1] != view[1] || id3FileSignature[2] != view[2])) {

                //Not an mp3
            }
        }

    })
    reader.readAsArrayBuffer(audioFile);
}

Here is some code that does similar to yours except you can select multiple files. 除了可以选择多个文件之外,这里有一些类似于你的代码。 #name == f.name and #type == f.type . #name == f.name#type == f.type I tested mine on android and its working on... 我测试了我的Android及其工作...

Android 4.1.2 | Android 4.1.2 | Chrome 42.0.2311.111 | Chrome 42.0.2311.111 | JS V8 4.2.77.15 JS V8 4.2.77.15

HTML HTML

<input type="file" id="files" name="files[]" multiple />
    <output id="list"></output>

JS JS

<script>
  function handleFileSelect(evt) {
    var files = evt.target.files; // FileList object

    // files is a FileList of File objects. List some properties.
    var output = [];
    for (var i = 0, f; f = files[i]; i++) {
      output.push('<li><strong>', escape(f.name), '</strong> (', f.type || 'n/a', ') - ',
                  f.size, ' bytes, last modified: ',
                  f.lastModifiedDate ? f.lastModifiedDate.toLocaleDateString() : 'n/a',
                  '</li>');
    }
    document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>';
  }

  document.getElementById('files').addEventListener('change', handleFileSelect, false);
</script>

Result 结果

  • List item Untitled%20Diagram.xml (text/xml) - 1224 bytes, last modified: 2/24/2015 列表项无标题%20Diagram.xml(text / xml) - 1224字节,最后修改时间:2015年2月24日

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM