CHECK FILE EXTENSION AT THE TIME OF UPLOADING

We want to check file extension before it is uploaded. The action should be executed all in the browser without using Ajax when we upload any file.

Associated HTML:

<%= f.file_field :file, required: true, :id => "shot_file" %>

<span>(pdf,doc,docx,ppt,pptx,ods,odp,odt)</span>
<div>
    <span class='file_ext_error text-danger'></span>
</div> 

Please take a look at the sample JS code below:

<script>
  $(document).on('change','#shot_file', function(){
    file_name = $(this).val();
    ext = file_name.split('.');
    ext = ext[ext.length - 1];
    valid_ext = ['pdf','msword','doc','docx','ppt','pptx','ods','odp', 'odt', 'xlsx']
    if (valid_ext.indexOf(ext) >= 0) {
      $('.actions input').removeAttr("disabled");
      $('.file_ext_error').html('');
    }
    else {
      $('.actions input').attr('disabled', 'disabled');
      $('.file_ext_error').html('Incorrect File Extension!');
    }
  });
</script>

Comments

Popular posts from this blog

psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

CARRIERWAVE - UPLOAD ANY MEDIA (AUDIO , VIDEO AND IMAGE ) FILE IN RAILS

SELECT COUNTRY WITH STATE AND IT'S CITIES RAILS

RAILS: UPLOADING PHOTOS VIA AMAZON S3 AND PAPERCLIP (UPLOADING FILES TO S3 IN RUBY WITH PAPERCLIP)

SEARCHING USING AJAX IN RAILS

E: Package 'aptitude' has no installation candidate

CURL COMMAND ON RUBY ON RAILS

CHECK PASSENGER RUBY PATH

UPLOAD ANY MEDIA FILE IN RAILS USING PAPERCLIP