Sometimes we need to find the extension (file-type) of a file at the time of uploading to apply some validation on it. We can do this by handling "change" event of input-file.
Example: In the below example I want to upload the file having extension jpeg,jpg,png,gif,tiff only. 
<input type="file" name="uploadFile" id="uploadFile" class="upload" title="Choose a file to upload"> 
<span id="fileName_error"></span>
<script>
$('#uploadFile').bind('change', function() {
              handleUpload();
          });
function handleUpload(){
          if($('#uploadFile').val() !='')
          {
              if($('#uploadFile').val().indexOf('.')>=0){
              //Getting extension of file here
              var ext = $('#uploadFile').val().split('.').pop().toLowerCase();
              if($.inArray(ext, ['jpeg','jpg','png','gif','tiff']) != -1) {
                      return true;
                  }
              }
              $('#fileName_error').text('Please upload the valid file(e.g jpeg,jpg,png,gif,tiff).');
              return false;
          }
          else
          {
              return true;
          }   
      }
</script>
Hope this will help you :)
                       
                    
0 Comment(s)