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>

Popular posts from this blog

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)

UNINITIALIZED CONSTANT PAPERCLIP::STORAGE::S3::AWS

CREATE API FOR USER SIGN UP, LOGIN AND CREATE PUBLISHER WITH CURRENT USER

HOW TO CALL RAILS API FROM SIMPLE HTML PAGE.

DEPLOYING PROJECT USING CAPISTRANO (CAPISTRANO IN RAILS)