Javascript 仅允许pdf、doc、docx格式的文件上载?

Javascript 仅允许pdf、doc、docx格式的文件上载?,javascript,jquery,Javascript,Jquery,我在href单击时触发文件上载。 我正在尝试阻止除doc、docx和pdf之外的所有扩展。 我没有得到正确的警报值 <div class="cv"> Would you like to attach you CV? <a href="" id="resume_link">Click here</a></div> <input type="file" id="resume" style="visibility: hidden">

我在href单击时触发文件上载。
我正在尝试阻止除doc、docx和pdf之外的所有扩展。
我没有得到正确的警报值

<div class="cv"> Would you like to attach you CV? <a href="" id="resume_link">Click here</a></div>
    <input type="file" id="resume" style="visibility: hidden">

…在输入字段上使用
change
事件更好地显示错误

更新资料来源:

var myfile="";

$('#resume_link').click(function( e ) {
    e.preventDefault();
    $('#resume').trigger('click');
});

$('#resume').on( 'change', function() {
   myfile= $( this ).val();
   var ext = myfile.split('.').pop();
   if(ext=="pdf" || ext=="docx" || ext=="doc"){
       alert(ext);
   } else{
       alert(ext);
   }
});
更新。

试试这个

 $('#resume_link').click(function() {
        var ext = $('#resume').val().split(".").pop().toLowerCase();
        if($.inArray(ext, ["doc","pdf",'docx']) == -1) {
            // false
        }else{
            // true
        }
    });
希望它能帮助你使用

<input name="Upload Saved Replay" type="file" 
  accept="application/pdf,application/msword,
  application/vnd.openxmlformats-officedocument.wordprocessingml.document"/>

惠拉

  • application/pdf
    表示
    .pdf
  • application/msword
    表示
    .doc
  • application/vnd.openxmlformats of cedocument.wordprocessingml.document
    表示
    .docx
相反

[编辑]请注意,
.dot
可能也匹配。

var file = form.getForm().findField("file").getValue();
var fileLen = file.length;
var lastValue = file.substring(fileLen - 3, fileLen);
if (lastValue == 'doc') {//check same for other file format}
$('surat_lampiran').bind('change',function(){
alerr=“”;
sts=假;
警报(此.files[0]。类型);
if(this.files[0].type!=“application/pdf”&&this.files[0].type!=“application/msword”&&this.files[0].type!=“application/vnd.openxmlformats of cedocument.wordprocessingml.document”){
sts=真;
alerr+=“Jenis文件bukan.pdf/.doc/.docx”;
}

});您只需使用正则表达式即可:

表格:


只允许PDF格式!
和java脚本验证:

<script>
    $('#submit').click(function(event) {
        var val = $('input[type=file]').val().toLowerCase();
        var regex = new RegExp("(.*?)\.(pdf|docx|doc)$");
        if(!(regex.test(val))) {
            $('.uploadExtensionError').show();
            event.preventDefault();
        }
    });
</script>

$(“#提交”)。单击(函数(事件){
var val=$('input[type=file]').val().toLowerCase();
var regex=new RegExp(“(*?”)\(pdf | docx | doc)$”;
如果(!(正则表达式测试(val))){
$('.uploadExtensionError').show();
event.preventDefault();
}
});

干杯

以下代码适用于我:

<input #fileInput type="file" id="avatar" accept="application/pdf,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document" />

application/pdf means .pdf
application/msword means .doc
application/vnd.openxmlformats-officedocument.wordprocessingml.document means .docx

应用程序/pdf-意思是.pdf
应用程序/msword表示.doc
application/vnd.openxmlformats-officedocument.wordprocessingml.document means.docx

对于浏览器窗口中只有扩展名为doc和docx的acept文件,请尝试以下操作

    <input type="file" id="docpicker"
  accept=".doc,.docx,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document">


我不认为这适用于所有浏览器。如果你使用HTML5,你可以使用属性
accept
。你的小提琴对我根本不起作用。您至少需要关闭
标记。@Rup
标记是自动关闭的。这是否回答了您的问题?不,不,不,这是正确的,我只是在上传syntaxOK的时候犯了一个错误,很公平。请修复您的小提琴,或者告诉我们您实际看到了什么错误(行为和脚本错误),或者两者都有-因为我们目前无法复制它。我的小提琴不工作,显示错误<代码>{“错误”:“请使用POST请求”}
…我只是无法获得扩展名的警报??没有语法错误。@MESSIAH,因为
元素重新加载iframe,所以
e.preventDefault()
解决了这个问题。如果要在DOCX中使用扩展名重命名文档,这将不起作用。您能突出显示您所做的更改吗?除了重构/重新排列,它看起来就像是
toLowerCase()
?@Rup if($.inArray(ext,[“doc”,“pdf”,“docx]”))==-1)看,你刚才从其他问题上扯下来了。。我刚刚看到了其他问题的确切答案。@MESSIAH up投票,如果你认为这有助于你解决问题,这会接受doc和docx吗…我不需要为此编写任何验证吗?看,如果客户端操作系统不知道
.pdf
-文件的mime类型,它不会显示供选择的
.pdf
-文件!您可以使用
navigator.mimeType
来评估此cornercase。Safari和IE9及更早版本不支持此属性。我怀疑IE10也没有,但我记不清具体情况。sais ie10接受。你是对的,ie9不接受。用户仍然可以在浏览器窗口中选择“所有文件”,然后可以上传他/她想要的任何文件。这并不验证格式,它只是在选择文件时显示在资源管理器窗口中的文件的“起始点”,只是一个提示,而不是一个严格的限制。您仍然可以选择具有不同扩展名的文件。在对话框中。我投了反对票。今天,我想取消我的否决票,因为我现在知道了
#fileInput
的含义。此外,我确实喜欢投赞成票,因为这确实是有帮助的,而且我在投反对票时错了!不幸的是,我不能对答案投赞成票,也不能取消反对票。因为
你的投票现在被锁定,除非这个答案被编辑。
。事实上,我确实认为这个答案很有帮助。
<input #fileInput type="file" id="avatar" accept="application/pdf,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document" />

application/pdf means .pdf
application/msword means .doc
application/vnd.openxmlformats-officedocument.wordprocessingml.document means .docx
    <input type="file" id="docpicker"
  accept=".doc,.docx,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document">