Question: How toi check either with casperjs or by javascript (afterwards) if my file download was successful?
I crawl some blogs and download the images, unfortunately it does not always download the image.
The casperjs script is run locally from my computer. I save all the filenames of the downloaded (or failed downloading) files into a json. The function itself does not give any information if it was performed successful: http://casperjs.readthedocs.org/en/latest/modules/casper.html#download
FAILED APPROACH 1:
function UrlExists(url)
{
var http = new XMLHttpRequest();
http.open('HEAD', url, false);
http.send();
return http.status!=404;
}
How do I check if file exists in jQuery or JavaScript? but I guess this works only on a server, it throws the error:
NETWORK_ERR: XMLHttpRequest Exception 101: A network error occured in synchronous requests.
How to check if file exists locally in JavaScript?
FAILED APPROACH 2:
I found also the following approach which does not work in casperjs or at least it doesn't display anything.
function checkImage (src) {
console.log("check");
var img = new Image();
img.onload = function(){console.log("yes");};
img.onerror = function(){console.log("no");};
img. src = src;
}
FAILED APPROACH 3:
The last approach gives me false, I guess that is also because the javascript is in sandbox:
function ImageExist(url)
{
var img = new Image();
img.src = '/Users/MasterG/Desktop/PROJEKTE/paleo-crawler/' + site + '/'+url;
console.log(img.src," - " ,img.height);
return img.height != 0;
}