2013-08-01 73 views
16

我正在触发href点击文件上传。
我想阻止除doc,docx和pdf之外的所有扩展名。
我没有得到正确的警报值。只允许pdf,doc,docx格式文件上传?

<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"> 

的Javascript:

 var myfile=""; 
     $('#resume_link').click(function() { 
      $('#resume').trigger('click'); 
      myfile=$('#resume').val(); 
      var ext = myfile.split('.').pop(); 
      //var extension = myfile.substr((myfile.lastIndexOf('.') +1)); 

      if(ext=="pdf" || ext=="docx" || ext=="doc"){ 
       alert(ext); 
      } 
      else{ 
       alert(ext); 
      } 
     }) 

MyFiddle ..its显示使用的输入字段change事件错误

+0

我不认为这会在所有的浏览器。如果您使用HTML5,则可以使用“accept”属性。 – putvande

+0

你的小提琴根本不适合我。您至少需要关闭标签''。 – Rup

+0

@Rup''标签自动关闭。 –

回答

13

更好。

更新的源:

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); 
    } 
}); 

更新jsFiddle

+0

no no no ...正确..我只是在上传语法时犯了一个错误 –

+0

好,够公平的。请修正您的小提琴,或者告诉我们您实际看到的错误(行为和脚本错误),或者两者 - 因为目前我们无法复制它。 – Rup

+0

我的小提琴不工作,它显示错误'{“错误”:“请使用POST请求”}'...我只是无法获得我的扩展警报??没有语法错误.. –

0

试一下这个

$('#resume_link').click(function() { 
     var ext = $('#resume').val().split(".").pop().toLowerCase(); 
     if($.inArray(ext, ["doc","pdf",'docx']) == -1) { 
      // false 
     }else{ 
      // true 
     } 
    }); 

希望这将有助于

+0

你能突出显示你改变了什么吗?除了重构/重新排列,它看起来就像'toLowerCase()'? – Rup

+0

@Rup if($。inArray(ext,[“doc”,“pdf”,'docx'])== -1)在这里看起来 –

+0

你刚刚从其他问题撕下了它..我刚刚看到了确切的答案在其他一些问题上。 –

42

您可以使用

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

whearat

  • application/pdf意味着.pdf
  • application/msword装置.doc
  • application/vnd.openxmlformats-officedocument.wordprocessingml.document装置.docx

代替。

[编辑]被警告,.dot也可能匹配。

+0

是这个跨浏览器兼容? –

+0

这将接受文档和docx ...我不需要为此写任何验证? –

+0

请参阅如果客户端操作系统不知道“.pdf”文件的MIME类型是什么,它将不会显示“.pdf”文件以供选择!你可以使用'navigator.mimeType'来评估这个角落。 –

0
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} 
0

$('#surat_lampiran').bind('change', function() { 
 
    alerr = ""; 
 
    sts = false; 
 
    alert(this.files[0].type); 
 
    if(this.files[0].type != "application/pdf" && this.files[0].type != "application/msword" && this.files[0].type != "application/vnd.openxmlformats-officedocument.wordprocessingml.document"){ 
 
    sts = true; 
 
    alerr += "Jenis file bukan .pdf/.doc/.docx "; 
 
} 
 
});

0

您可以简单地通过正则表达式使其:

形式:

<form method="post" action="" enctype="multipart/form-data"> 
    <div class="uploadExtensionError" style="display: none">Only PDF allowed!</div> 
    <input type="file" name="item_file" /> 
    <input type="submit" id='submit' value="submit"/> 
</form> 

和Java Script验证:

<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> 

干杯!

-1

下面的代码为我工作:

<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