只允许PDF,DOC,DOCX格式文件上传?

问题描述 投票:17回答:8

我触发的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">

使用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示值误差

javascript jquery
8个回答
14
投票

更好的输入字段中使用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);
   }
});

更新jsFiddle


55
投票

您可以使用

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

尝试这个

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

希望这将有助于


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>

和JavaScript验证:

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

干杯!


0
投票

对于只接受在资源管理器窗口,延长DOC文件和DOCX试试这个

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

-2
投票

下面的代码为我工作:

<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
© www.soinside.com 2019 - 2024. All rights reserved.