0

I am attempting to check that a file exists on the server but this code always returns that the file exists, although in fact file does not exist. Can anyone help me solve this my problem?

var file_path = this.model.get('image_path');
var path = file_path.substring(0, file_path.lastIndexOf('/') + 1);
var file_name = file_path.substring(file_path.lastIndexOf('/') + 1, file_path.lastIndexOf('.'));
var url = path+file_name+'.ad7';
$.ajax({
    url: url, //or your url
    type: 'HEAD',
    success: function(data){
        alert('exists');
        console.log(data);
    },
    error: function(data){
        alert('does not exists');
    },
});
zinismain
  • 11
  • 1

1 Answers1

0
$.ajax({
    url:'http://www.example.com/somefile.ext',
    type:'HEAD',
    error: function()
    {
        //file not exists
    },
    success: function()
    {
        //file exists
    }
});

From here: How do I check if file exists in jQuery or JavaScript?

Community
  • 1
  • 1
DDan
  • 8,068
  • 5
  • 33
  • 52