0

I try to check if an image is available in my mobile PhoneGap App with the following code:

function imageExists(image_url){

    var http = new XMLHttpRequest();

    http.open('HEAD', image_url, false);
    http.send();

    return http.status != 404;

}

The images are stored on the device and some images are missing. But the returned status is always 200 even if the image doesn't exist. I found the solution here: Check if image exists on server using JavaScript? It seems to work for everyone. What am i missing?

Edit: Like i say in my question the suggested solustion doesn't work and i want to know why? That's no possible dublication. Please read my question..

Community
  • 1
  • 1
Jonas
  • 2,139
  • 17
  • 38
  • 1
    Possible duplicate of [Check if image exists on server using JavaScript?](http://stackoverflow.com/questions/18837735/check-if-image-exists-on-server-using-javascript) – Bhojendra Rauniyar Mar 02 '16 at 08:42
  • You post a possible duplication of the exact link i link in my question? – Jonas Mar 02 '16 at 08:43
  • @BhojendraNepal could you please explain me how your dublication could even help me in the slightest way? – Jonas Mar 02 '16 at 08:46
  • probably he didn't read your answer correctly @JonasSchafft ! – Pedram Mar 02 '16 at 08:47

1 Answers1

0

The best way to do this would be to use the File Plugin and call the getFile method - see here for more info on usage. Here is a quick look at what you will want to be using as a starting point:

function onInitFs(fs) {

  fs.root.getFile('filename.jpg', {create: false, exclusive: false}, 
     function(fileEntry) {
          //file exists
          console.log("Woohoo the file exists!");
     }, fileErrorHandler);
}

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onInitFs, errorHandler);

function fileErrorHandler(){
     //file doesn't exist
     //now go get the file
     callYourGetFileHere();
}
whodeee
  • 617
  • 5
  • 18