2013-02-13 30 views
12

Tôi đang sử dụng uploadify để tải lên tệp bằng Codeigniter. Và trước khi tải lên các tập tin tôi chỉ cần kiểm tra xem phần mở rộng tập tin là chính xác là đúng hay không. Tôi đã thử với http://jquery.bassistance.de/ và cũng http://forum.jquery.com/jquery - Kiểm tra tiện ích mở rộng tệp trước khi tải lên

Bur không có kết quả phù hợp. Bất cứ ai có thể vui lòng cho tôi biết làm thế nào tôi có thể làm như vậy?

Cảm ơn trước ...

+1

có bạn kiểm tra http://jquery.bassistance.de/validate/demo/file_input.html –

+0

Không, nó thật sự tốt đẹp. Bạn có thể vui lòng cho tôi biết mã nguồn có sẵn cùng với bản demo không? – V15HM4Y

+0

http://jquery.bassistance.de/validate/jquery-validation-1.11.0.zip –

Trả lời

13

Tôi muốn cảm ơn những người đã đăng câu trả lời, nhưng ông đã xóa bài. Chúng ta có thể làm như thế này.

$("#yourElem").uploadify({ 
    'uploader': ..., 
    'script': ... 
    'fileExt' : '*.jpg;*.gif;', //add allowed extensions 
    ....., 
    'onSelect': function(e, q, f) { 
     var validExtensions = ['jpg','gif']; //array of valid extensions 
     var fileName = f.name; 
     var fileNameExt = fileName.substr(fileName.lastIndexOf('.') + 1); 
     if ($.inArray(fileNameExt, validExtensions) == -1){ 
      alert("Invalid file type"); 
      $("#yourElem").uploadifyCancel(q); 
      return false; 
     } 
    } 
}); 

Cảm ơn cho câu trả lời, nó thực sự làm việc ...

+0

Đơn giản và hữu ích. Cảm ơn. – QMaster

+0

nhận lỗi f là không xác định – OBAID

34

Nếu bạn muốn làm điều đó mà không cần plugin bạn có thể sử dụng sau.

javascript, sử dụng jQuery:

$(document).ready(function(){ 
    $("#your_form").submit(function(submitEvent) { 

     // get the file name, possibly with path (depends on browser) 
     var filename = $("#file_input").val(); 

     // Use a regular expression to trim everything before final dot 
     var extension = filename.replace(/^.*\./, ''); 

     // Iff there is no dot anywhere in filename, we would have extension == filename, 
     // so we account for this possibility now 
     if (extension == filename) { 
      extension = ''; 
     } else { 
      // if there is an extension, we convert to lower case 
      // (N.B. this conversion will not effect the value of the extension 
      // on the file upload.) 
      extension = extension.toLowerCase(); 
     } 

     switch (extension) { 
      case 'jpg': 
      case 'jpeg': 
      case 'png': 
       alert("it's got an extension which suggests it's a PNG or JPG image (but N.B. that's only its name, so let's be sure that we, say, check the mime-type server-side!)"); 

      // uncomment the next line to allow the form to submitted in this case: 
//   break; 

      default: 
       // Cancel the form submission 
       submitEvent.preventDefault(); 
     } 

    }); 
}); 

HTML:

<form id="your_form" method="post" enctype="multipart/form-data"> 
    <input id="file_input" type="file" /> 
    <input type="submit"> 
</form> 
22

Bạn có thể đạt được điều này bằng JQuery

HTML

<input type="file" id="FilUploader" /> 

JQuery

$("#FilUploader").change(function() { 
     var fileExtension = ['jpeg', 'jpg', 'png', 'gif', 'bmp']; 
     if ($.inArray($(this).val().split('.').pop().toLowerCase(), fileExtension) == -1) { 
      alert("Only formats are allowed : "+fileExtension.join(', ')); 
     } 
    }); 

Để biết thêm thông Click Here

+3

Thay vì 'split()' sẽ là tốt đẹp để sử dụng 'lastIndexOf ('.')' Để đảm bảo bạn đang sử dụng dấu chấm cuối cùng, đề cập đến phần mở rộng. –

0
 function yourfunctionName() { 
      var yourFileName = $("#yourinputfieldis").val(); 

      var yourFileExtension = yourFileName .replace(/^.*\./, ''); 
      switch (yourFileExtension) { 
       case 'pdf': 
       case 'jpg': 
       case 'doc': 
        $("#formId").submit();// your condition what you want to do 
        break; 
       default: 
        alert('your File extension is wrong.'); 
        this.value = ''; 
      } 
//    $("#upload_" + id).submit(); 
     } 
3

Đây là một mã đơn giản để xác nhận javascript, và sau đó xác nhận nó sẽ làm sạch các tập tin đầu vào.

<input type="file" id="image" accept="image/*" onChange="validate(this.value)"/> 

function validate(file) { 
    var ext = file.split("."); 
    ext = ext[ext.length-1].toLowerCase();  
    var arrayExtensions = ["jpg" , "jpeg", "png", "bmp", "gif"]; 

    if (arrayExtensions.lastIndexOf(ext) == -1) { 
     alert("Wrong extension type."); 
     $("#image").val(""); 
    } 
} 
1

Nếu bạn không muốn sử dụng $(this).val(), bạn có thể thử:

var file_onchange = function() { 
    var input = this; // avoid using 'this' directly 

    if (input.files && input.files[0]) { 
    var type = input.files[0].type; // image/jpg, image/png, image/jpeg... 

    // allow jpg, png, jpeg, bmp, gif, ico 
    var type_reg = /^image\/(jpg|png|jpeg|bmp|gif|ico)$/; 

    if (type_reg.test(type)) { 
     // file is ready to upload 
    } else { 
     alert('This file type is unsupported.'); 
    } 
    } 
}; 

$('#file').on('change', file_onchange); 

Hope this helps!

0

Mã sau cho phép tải lên các tệp gif, png, jpg, jpeg và bmp.

var extension = $('#your_file_id').val().split('.').pop().toLowerCase(); 

if($.inArray(extension, ['gif','png','jpg','jpeg','bmp']) == -1) { 
    alert('Sorry, invalid extension.'); 
    return false; 
} 
Các vấn đề liên quan