Results 1 to 4 of 4

Thread: Ajax Test for File Exists?

  1. #1
    Join Date
    Feb 2006
    Posts
    223
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Default Ajax Test for File Exists?

    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.....

  2. #2
    Join Date
    Feb 2006
    Posts
    223
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Default

    Hummm, no replies yet.....

    Will this work? I found code on the web and modified it only a little:
    HTML Code:
    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:
    HTML Code:
    (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.

  3. #3
    Join Date
    Feb 2006
    Posts
    223
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Default

    Still working on this. Found a post: http://www.dynamicdrive.com/forums/a...hp/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:
    HTML Code:
    objAJAX.send(null);
    will have to rewrite the function..... Still hoping to get this to work.....

  4. #4
    Join Date
    Feb 2006
    Posts
    223
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Default

    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:
    HTML Code:
    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 //
    }
    }
    }
    [COLOR="Red"]alert(iB)[/COLOR]
    	return iB;
    }

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •