0

要素で、<input type="file" >拡張子 .srt のファイルを選択し、javascript を使用してファイルの種類を判別しています。

var file=document.getElementById('fselect').files[0];
success = file.type =='application/x-subrip'? true : false;
console.log('selected a  file of type='+file.type);
console.log('selected a subtitle file='+success);

ただし、期待どおりの結果が得られるのはchrome 18.0.1025.168

selected a  file of type= application/x-subrip
selected a subtitle file= true

しかし、firefox 12firebugがインストールされていると、私は得る

selected a  file of type= 
selected a subtitle file= false

私はこれに混乱しています..どうすれば両方のブラウザでファイルの種類を一貫して判断できますか?

4

2 に答える 2

0

HTML5のファイルAPIを使用できます。現在、IE9を含むすべての最新ブラウザでサポートされています。

http://www.html5rocks.com/en/tutorials/file/dndfiles/

function handleFileSelect(evt) {
    var files = evt.target.files;                  // file list
    for (var i = 0, f; f = files[i]; i++) {
        if(f.type){
            console.log(f.type); 
        }else{
            var match = f.name.match(/^.+(\.[^\.]+)$/);
            if(match) {
                console.log(match[1]);
            }else{
                console.log('no MIME type nor extension');
            } 
        }
    }
}

document
    .getElementById('files')
    .addEventListener('change', handleFileSelect, false);

 

<input type="file" id="files">

ライブデモ:http: //jsfiddle.net/DerekL/f2WmJ/

これは標準であるため、ブラウザ間で同じである必要があります。

于 2012-05-17T03:59:19.883 に答える
0
<input type="file" onChange="showExcelData(this)">

function showExcelData(event)
{
    var file=event.value;
    file=file.substr(file.lastIndexOf("/")+1,file.length);
    file=file.substr(file.lastIndexOf(".")+1,file.length);
    console.log(file);

    /* You Can Return the Data or Anything You Want to Do */
}
于 2017-09-11T14:16:07.600 に答える