Check if file exists using Javascript or jQuery

Asked

Viewed 5,034 times

0

How can I check using Javascript or jQuery that a file exists ?

OBS: I opened the question and already answered.

  • What you’ve already done?

  • @Bigown Has in the answer below.

  • :D is that I analyzed in the queue, showed nothing

  • Exists on the server or the computer of who is viewing the page?

1 answer

1


Below are the means I found.

Javascript

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

jQuery

$.ajax({
    url:'http://www.exemplo.com.br/arquivo.txt',
    type:'HEAD',
    success: function() {
        //arquivo existe
    }
    error: function() {
        //arquivo não existe
    },
 });
  • that’s the answer?

  • 2

    this checks a url and not the existence of a file...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.