Wild Lion
Wild Lion

Reputation: 53

Tried all jquery methods and js to check for file exist but all doesn't work

Tried all jquery methods and js to check for file exist but all doesn't work all give me that any file that exist or doesn't on my server / with my domain that it does exist although some dont... idk why it say that all does although they dont?!!! maybe something wrong with my server? idk :( i need help tried all of that

$.get('http://MyUrl/file.wav')
.done(function() { 
    alert('exists');

}).fail(function() { 
    alert('does not exist');

})

&

$.ajax({
  url: 'http://MyUrl/file.wav', //or your url
  type: 'GET',
  success: function(data){
    alert('exists');
  },
  error: function(data){
    alert('does not exist');
  },
})

&

    function doesFileExist(urlToFile)
    {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', 'http://MyUrl/file.wav', false);
        xhr.send();

        if (xhr.status == "404") {
            //return false;
    console.log('not exist');
        } else {
            //return true;
            console.log('file with that name exists exists');

        }}

&

$.ajax({
    url:'http://MyUrl/file.wav',
    type:'HEAD',
    error: function()
    {
        //file not exists

    },
    success: function()
    {
        //file exists
      console.log('file with that name exists exists');
    }
});

idk what else i do really but it frustrate me that it always give me exists or true no matter file exists or not.....

Upvotes: 0

Views: 843

Answers (3)

Tushar
Tushar

Reputation: 87233

Create a new route for ajax

Server side:

echo json_encode(new Array(exists => file_exists(filePath + $_POST['filename]));

Client side:

$.ajax({
    url:'http://MyUrl/fileexists.php', // new route
    type:'POST', // post data
    data: { filename: 'new.wav' }, // data to post
    dataType: 'json', // returned data type
    error: function()
    {
        //file not exists
    },
    success: function(resp)  {
        //file exists?
      console.log('file with that name exists exists?' + resp.exists);
    }
});

Upvotes: 1

guest271314
guest271314

Reputation: 1

$.get("/path/to/file/")
.always(function(data, textStatus, errorThrown) {
  // if file does not exist, or `error` , log `textStatus`, `errorThrown`
  if (textStatus !== "success") console.log(textStatus, errorThrown);  
  // else, log response `data`
  else console.log(data);
});

Upvotes: 1

pisamce
pisamce

Reputation: 533

I guess you are using rewrite rules on your server? If yes, most likely your server is rewriting your requests for non-existing files and you are getting response from some script which handles requests. Did you check response from your server? For example, open developer tools in chrome browser and then open the url of non-existing file in chrome, what do you see in network tab of developer tools? If you are always getting 200 http response, you will need to change your rewrite conditions or modify your script to check for file existence and send back 404 if file does not exist.

Upvotes: 1

Related Questions