Thursday 12 May 2016

File upload validation

I have explained here about uploading a file from UI (File upload and sending data to backend using angular js) .We can restrict user to upload only a war or zip file by checking the extension of file. Let us check with an example



function validateFile(){  
  var fileName = 'sample.zip';
  var ext = fileName.substring(fileName.lastIndexOf('.') + 1);  
  if(ext.toLowerCase() == 'war')
  {
     return true;
  }
  else
  {
     return false;
  }
}

if(validateFile())
{
  //upload
}
else
{
  //show a message 
}


Same code you can use for any extension .Let us check same code for image upload


function validateImage(){  
  var imageName = 'sampleImage.png';
  var ext = imageName.substring(imageName.lastIndexOf('.') + 1);  
  if(ext.toLowerCase() == 'png')
  {
     return true;
  }
  else
  {
     return false;
  }
}

if(validateImage())
{
  //upload
}
else
{
  //show a message 
}

Using the above validator function you can easily find out the extension of any files or images

1. File upload and sending data to backend using angular js

2. How to download file from backend to UI

3. Add and remove active class on click Angular

No comments :

Post a Comment