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>
Post a Comment

Popular posts from this blog

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

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

SELECT COUNTRY WITH STATE AND IT'S CITIES RAILS

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

Building native extensions. This could take a while... ERROR: Error installing mysql: ERROR: Failed to build gem native extension.

HOW TO CALL RAILS API FROM SIMPLE HTML PAGE.

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