在<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
但是在安装了firebug的firefox 12
中,我得到了
selected a file of type=
selected a subtitle file= false
我对此感到困惑。我可以在两种浏览器中一致地确定文件的类型吗?
答案 0 :(得分:0)
您可以使用HTML5的文件API。目前,每个现代浏览器都支持它,包括IE 9。
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/
在浏览器中应该是相同的,因为它是标准的。
答案 1 :(得分: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 */
}