欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

js 判断 上传文件的类型

程序员文章站 2024-02-19 12:32:10
...

工作需要 判断上传的文件类型 为图片(gif,jpg,png)

view:

 

 
 <% form_for :complain_evidence,:url => {:action => "upload_complain_evidence",:id => @order.seq_no},:html => {:id => 'f_e',:multipart => true} do |c| %>
 
<p><%=file_field :attachment,:uploaded_data%>(picutre only)</p>

<input type="button" value="Submit" onclick="check_evidence(this.form.attachment_uploaded_data.value);return false;"/>


 

js:

 

 function check_evidence(file){
    var str = file.substring(file.lastIndexOf('.')+1);
    if(file != '' && (str == 'png' || str == 'gif' || str == 'jpg')){
      jQuery('#f_e').submit();
    }else if(file == ''){
      alert('please select evidence file to upload!');
    }else if(str != 'png' && str != 'gif' && str != 'jpg'){
      alert('please upload image file! ');
  }