Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to find the extension of a file to be uploaded in JSP?

    • 0
    • 1
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 427
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: