PDA

View Full Version : Ajax Test for File Exists?



Strangeplant
11-06-2007, 06:11 PM
I'm displaying a subset of image files from a large directory, and the subset was selected from the directory by well-defined name rules using javascript and input boxes. BUT, every once in a while, one file(s) of the known subset is missing, so I need to redo my display array if this is the case. I gotta test if the files all exist before I present them. Looking on the web, I see that there are some javascript 'file exist' functions, but some don't work in FF because they need ActiveX control, etc. If I use php, I would have to call it once for each display/search inside the active html - and so that won't work. How about Ajax? Using <<, <, >, >> buttons and javascript functions, so I really must have a valid array early on.....

Thanks in advance for your advice.....

Strangeplant
11-07-2007, 06:18 PM
Hummm, no replies yet.....

Will this work? I found code on the web and modified it only a little:
function testExists(strFile) {
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) {
return "false"; // file exists //
else
return "true"; // file doesn't exist //
}
}
return "true"; // can't define Ajax object - don't chance it //
}
}and that gets called here:
(snip)

/// Correct imageurl array for any missing elements //
for (m=3;m<=imagetotl-1;m++) {
while (testExist(imageurl[m] && m<=imagetotl-1) {
for (o=m;o<=imagetotl-1;o++){ // shift array //
imageurl[o]=imageurl[o+1];
imagetotl--;
}
}
}
(snip)I just need to make sure that I define the imageurl array one larger than I need to hold the urls.

Strangeplant
11-07-2007, 07:44 PM
Still working on this. Found a post: http://www.dynamicdrive.com/forums/archive/index.php/t-9434.html

Indicates that one statement that I removed shouldn't have been, so I'm missing something like this in the function testExists(), before return:
objAJAX.send(null);will have to rewrite the function..... Still hoping to get this to work.....

Strangeplant
11-13-2007, 05:00 PM
Have been working on this now and then when I get a free moment, and have something working, but not right. It works fine with the alert statement, but will not work without it, meaning that the function isn't evaluated correctly by my array restructuring routine as true/false. What's wrong? The function is this, and iB is declared as a global:
function testExists(strFile) { // returns false if exists
var fileObj = null;
if(window.ActiveXObject) { // if IE
try {
fileObj = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
fileObj = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
fileObj = false;
}
}
} else if(window.XMLHttpRequest) { // if Mozilla, Safari, etc.
fileObj = new XMLHttpRequest();
}

if(fileObj) {
bustcacheparameter=(strFile.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime();
fileObj.open("HEAD", strFile+bustcacheparameter, true);
fileObj.send(null);
fileObj.onreadystatechange = function() {
if (fileObj.readyState == 4 && fileObj.status == 200) {
iB = false; // file exists //
}
else {
iB = true; // file doesn't exist //
}
}
}
alert(iB)
return iB;
}