2011-10-31 26 views
1

如何使用jQuery和例如alert它获得的文件类型的用户选择(在某些<input type="file" name="datafile">)?至少它的延伸,但像image这样的分组类型将是篦。有没有任何jQuery插件?如何使用jQuery获取用户选择文件的类型?

+3

HTTP: //stackoverflow.com/questions/4581308/jquery-or-javascript-get-mime-type-from-url 我不知道,但是这可能是乌拉圭回合的答案 – Kris

回答

4
$('input[type=file]').change(function(e){ 
    var file = $(this).val(); 
    var ext = file.split('.').pop(); 
    alert(ext); 
}); 

试试这个:)

3

只需提供一个id为您输入并使用该

document.getElementById(ID).files[0].type 
+0

这不是在IE 9和更低的支持。不过,它确实在IE 10预览中有效。 –

2

试试这个让你的文件扩展名。

var fileName = "Ram.png"; 
alert(fileName.substring(fileName.lastIndexOf('.') + 1)); 
1

你为什么不使用这样的事情:

$('input:file').change(function() { 
    var ext = this.value.split('.').pop(); 
    console.log(ext); 
}); 

,如果你想使用一个 “多” 字段,你可以使用这样的事情:

$('input:file').change(function() { 
    var $this = $(this); 
    if ($this.prop('multiple')) { 
     for (var i=0; i<this.files.length; i++) { 
      var ext = this.files[i].fileName.split('.').pop(); 
      // this.files[i].extension = ext; // usefull for future use 
      console.log(ext); 
     } 
    } 
    else {   
     console.log(this.value.split('.').pop()); 
    } 
}); 
相关问题