PDA

View Full Version : Looking for a way to detect if a file exists


DanLap
03-15-2003, 03:30 AM
Hi there,

Is there a way to detect if a file (txt, html, js, etc.) exists on either a local hard drive or on a server using javascript?

I want to use the existence of a file as a condition to do something. The file woud exists on the server but not on the local drive (or vice-versa) and depending on the result of the text, a button or some text would show up or not.

Thanks

COBOLdinosaur
03-15-2003, 07:04 PM
It would be a security hole if you could access the local hard drive. You could do it with activeX, but the user would have to agree to the download. I would never accept such a control on a web page.

liorean
03-15-2003, 08:00 PM
For local files, you can use ActiveX, Java, a custom plugin or do a general security override for mozilla followed by XMLHTTPRequest. All of them will ask the user for permission, unless you are using one of the loophole in the ActiveX/Plugin mechanism or the MS Java VM (There's several loophols that can be used for this, one of them the infamous download-and-install/upgrade-activex-component-without-user-permission loophole used in Alexa and other tools).

For checking for existence on a server, that's easier. Simply use the XMLHTTPRequest interface with the right request (see my File constructor in <http://codingforums.com/showthread.php?s=&threadid=16378>).

Using the File constructor, you can do the following:

var f=new File;
f.open('HEAD',[String url]);
f.send();
var headers=f.getAllResponseHeaders();
// This returns ALL http headers. You'll have to sift through them
// Also note that this won't work in nn4, iem, safari/konqueror, opera, icab and other minor browsers.


For more on the http headers see [RFC2068] (http://ietf.org/rfc/rfc2068.txt).

joh6nn
03-15-2003, 10:29 PM
the only kinds of files that javascript can detect, without the kind of outside help mentioned above, are images.

images in javascript have onerror events, that fire when any problem in loading the picture occurs, such as, the picture doesn't exist.

sample code might look like

var pic = new Image();
pic.onerror = function() {
alert("the picture " + this.src + " could not be loaded.");
}
pic.src = "somepic.gif";

note that the onerror event has to be created, before you assign the picture it's src; if you do it the other way around, the picture could finish loading, before the onerror event is defined, and the onerror event might not fire, even if the picture doesn't exist.

in theory, you could combine javascript with a cgi script, to check and see if a file exists. you'd have to set up a cgi script that checks to see if a file passed by the GET method exists. then, you could use javascript to load a url into an Image object, like in the above example. if you loaded the cgi script's url into the above example, then the cgi could check to see if the file exists. if the file exists, you return an image, the javascript announces that everything's okay. if the file doesn't exist, the cgi returns a url for a picture that doesn't exist. the javascript onerror event would fire, and you'd know that the file wasn't there.

hope that's helpful.