{
If you're like me, you spend a lot of time using System.IO and can easily check for the existence of a file on the filesystem with a File.Exists(@"C:\spam\eggs.file")
It's a little surprising that I hadn't run into it before but a few weeks ago I needed that same functionality with javascript. I found a few things online and what works best is using AJAX to make a request and check the HTTP status for whether or not the request failed. My approach was to use Prototype and something like this:
function checkForFile(imgName, displayHolder){
    new Ajax.Request('Repo/' + imgName + '.jpg',   
        {
        method:'get',
        onSuccess: function(transport){   
            var response = transport.status || "no response text";   
            if(parseInt(response) == 200){
                displayHolder.src = 'Repo/' + imgName + '.jpg';
                displayHolder.style.display='block';
            }
        },     
        onFailure: function(){ 
            displayHolder.src = 'Repo/NoImage.gif';
            displayHolder.style.display='block';
        }   
    });     
}
Works like a charm.
 
 

 
 Posts
Posts
 
 
No comments:
Post a Comment