...

View Full Version : testing if file exist before doing a action



nkamp
07-04-2006, 11:59 AM
Hello,

I'm maintaining a application. And there is opportunity to open file's (like .doc, .jpg, .txt, etc.).


setFrame('frm_file', '/ALGEBESTANDEN/' + file_link);

(The file_link is comming out from the database)

But if the file not exist anymore, then I get a javascript error.

Is there a simple opportunity to test if the file exist? It seems to me that this must be possible because I can the file open with Javascript as well, or...?

thanks in advance,

Nico

martin_narg
07-04-2006, 06:16 PM
function fileIntoFrame(strFile, strFrame) {
var objAJAX = null;
if(window.ActiveXObject) {
try {
objAJAX = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
objAJAX = new ActiveXObject("Microsoft.XMLHTTP");
} catch(E) {
objAJAX = false;
}
}
} else if(typeof XMLHttpRequest != "undefined") {
objAJAX = new XMLHttpRequest();
}

if(objAJAX) {
objAJAX.open("GET", strFile, true);
objAJAX.onreadystatechange = function() {
if(objAJAX.readyState == 4 && objAJAX.status == 200) {
setFrame(strFrame, strFile); /* this uses your existing setFrame function */
}
}
objAJAX.send(null);
}
}

/* Usage */
fileIntoFrame("/myFolder/myFile.txt", "myFrame");

Hope this helps

m_n

nkamp
07-05-2006, 10:19 AM
Hello Martin,

Thank you for you're reaction.
This is new to me so let's begin at the beginning.
The first try is executed:


try {
alert(window.ActiveXObject);
objAJAX = new ActiveXObject("Msxml2.XMLHTTP");
}

But what is the meaning of this?

Then:


alert(objAJAX) ;
if(objAJAX) {
objAJAX.open("GET", strFile, true);

if(objAJAX) is a object! (I saw it with the alert). Did you expect this or did you expect true or false?

What does objAJAX.open do?

And on the end, the objAJAX.readystate is 4 and objAJAX.status is 200 even if the file doesn't exist. So on the end I have the same result as before.

By the way the function setFrame is nothing more or les then:


function setFrame(framenaam, pagina) {
parent[framenaam].location.href = pagina;
}


Nico

martin_narg
07-07-2006, 11:06 AM
SImply put, it's an XMLHTTP call (w3schools ajax tutorial (http://www.w3schools.com/ajax/default.asp)) that test the existance of the file before populating the frame with that file.

Breaking down the script, the first part with the try .. catch statements is merely creating a cross-browser xml http object and assigning that to a variable (objAJAX). This is explained further in the above link.

The xml http object has various methods and properties - again these are explained in more detail in the link, but essentially, the open() method of the cml http object is the bit that asks for the file. There are various http status code definitions - the best known being 404 (not found) wikipedia - 404 (http://en.wikipedia.org/wiki/HTTP_404) - but you only want to populate the frame if the file is found - which is http status 200. The readyState == 4 bit of is simply property of the xml http object that turns to 4 when loading is complete (if the page has been successfully loaded, or can't be found, or if there's another error, etc).

If you want to read more about AJAX - then this is another fantastic resource/tutorial: Mozilla AJAX Tutorial - Getting started (http://developer.mozilla.org/en/docs/AJAX:Getting_Started)

Hope this helps

m_n



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum