0

I tried almost all solutions but they all come back on a success level as if it exists when it does not. Can someone help?

I try it on files that DONT exist but it comes back as if it exists which makes no sense.. I even output the playlisturl and comes back exactly how i want it but my guess is, its not search right?

What i tried: //My Vars

var playlistname = "index";
var playlisturl = "/var/lib/mpd/playlists/"+ playlistname + ".m3u";

Tried:

function doesFileExist(urlToFile)
{
    var xhr = new XMLHttpRequest();
    xhr.open('HEAD', urlToFile, false);
    xhr.send();

    if (xhr.status == "404") {
        console.log("File doesn't exist");
        alert('does not exist');
        return false;
    } else {
        console.log("File exists");
        alert('exists: ' + playlisturl);
        return true;
    }
}
doesFileExist(playlisturl);

And:

$.UrlExists = function(url) {
    var http = new XMLHttpRequest();
    http.open('HEAD', url, false);
    http.send();
    return http.status!=404;
}

if($.UrlExists(playlisturl)){
    alert('exists: ' + playlisturl);
}else{
    alert('does not exist');
}

And:

$.ajax({
    url: playlisturl,
    type:'HEAD',
    error: function()
    {
        alert('does not exist');
    },
    success: function()
    {
        alert('exists: ' + playlisturl);
    }
});
William
  • 1,009
  • 15
  • 41
  • Perhaps this will help http://stackoverflow.com/a/9970672/673457 – Ted Whitehead Dec 01 '16 at 17:08
  • But if the file is there it will download will it not? I just want to check and if there dont do this ortherwise do this? Not sure if that will do it or not. – William Dec 01 '16 at 17:14
  • So your last example works for me and doesn’t download the file. Demo https://tedw.github.io/file-detect-test/ Are you serving those `m3u` files? – Ted Whitehead Dec 01 '16 at 18:26
  • @TedWhitehead The m3us are on my server in the directory stated in my question. I tried myself but doesnt work. – William Dec 05 '16 at 13:45
  • This could be an issue with your server. What error msg are you getting? – Ted Whitehead Dec 05 '16 at 20:19

0 Answers0