Re: checking for a file



On Tue, 16 May 2006 14:46:07 -0700, Eric wrote:

Is it possible to check for existence of a file from a web page using
javascript? I am trying to come up with a routine that i can call from the
rest of my javascript code that will check if a particular file exists on
disk at the server. ie bool FileExists("http://myServer.com/File.gif";);
Any ideas on how to do this?

As far as I know, there's no way to check if a file exists, but you can
try to load an image and respond appropriately depending on whether or not
it loads successfully, using the onload and onerror events. The only
problem with this is that you'll need to redesign your system to be event
based, rather than load the images in a loop. Depending on what you want
to do, this may or may not be easy. You could try something like:

<html>
<head>
<script type="text/javascript">

// ImageList class - pass a list of URLs,
// call 'get', and then callbacks will be
// called as the loading progresses -
// on_each when each image is successfully loaded,
// on_error if there is an error, and
// on_complete when all images have been loaded.
// Each callback should return 'true' if the next
// URL in the list should be loaded, or false
// if the loading should be aborted.

function ImageList(urls) {
// A list of URLs that have not yet been loaded
this.urls = urls;

// A list of images that have been successfully loaded
this.images = new Array();

// Override these three methods in your own instances
this.on_each = function (url, img) { };
this.on_complete = function () { };
this.on_error = function (url) { };

this.get = function() {
this.get_next_image();
}

this.get_next_image = function() {
var url = this.urls.shift();
if( !url ) {
this.on_complete();
return;
}
var img = new Image();
var il = this;
img.onload = function () {
il.images.push(img);
if( il.on_each(url, img) )
il.get_next_image();
}
img.onerror = img.onabort = function () {
if( il.on_error(url) )
il.get_next_image();
}
img.src = url;
return true;
}
return this;
}

// This will attempt to load four images, but it will fail to load the
// third, and abort at that point.

function test() {
image_urls = [
"http://images.google.com/intl/en/images/logo.gif";,
"http://images.google.com/intl/en_ALL/images/images_hp.gif";,
"http://test.invalid/no-image-here";,
"http://groups.google.com/groups/img/groups_home.gif";
];

var il = new ImageList(image_urls);

// Override the error callback - returns false,
// so no further images will be loaded
il.on_error = function (url) {
document.write("Error at "+url+"<br>");
return false;
}

// Override the callback for each image load,
// returns true, so the next image will be
// downloaded
il.on_each = function (url, img) {
document.write("Got image from "+img.src +"<br>");
return true;
}

// Override the callback which will be invoked when
// all images have been successfully loaded
il.on_complete = function () {
document.write("Complete<br>");
document.write("Loaded "+il.images.length+" images<br>");
}
il.get();
}
</script>
</head>
<body onload="test()">
</body>
</html>


.



Relevant Pages

  • Re: Attention David F
    ... I do have a 56k modem, but because of the distance from the switching ... Both of your home pages load relatively quickly for me, ... more images you have the longer it takes for a page to load. ... comparison from a person that had built a 200+ page site in Publisher, ...
    (microsoft.public.publisher.webdesign)
  • Re: Rapid gfx display Qs
    ... I think with Autoredraw False you are effectively timing the total time both of loading the picture from disk into memory and also the time taken for the OS to dump that picture to the screen. ... In contrast, with Autoredraw True, you are timing only the time taken to load the picture from disk into memory because the VB Autoredraw system itself will dump the property to the actual display just after your code has finished. ... I get a time of about 79 msecs just to load the picture into a StdPicture object, even though the file size of the jpg is very much smaller, only about one tenth of the file size of the equivalent bmp. ... Much of course depends on the nature of your images, and it is hard to comment any further really without knowing what sort of images they are. ...
    (microsoft.public.vb.general.discussion)
  • Re: CONCERNED About the Publisher 2007 index file size
    ... While you will find some expert web designers, ... Publisher effectively takes a print document and ... approximately 23 seconds to load fully. ... so people will have something to read while the images load. ...
    (microsoft.public.publisher.webdesign)
  • Re: Attention David F
    ... resampling and resizing images to 96 dpi in Publisher. ... I'm glad my pages load decently. ... Web Plus does seem to offer a lot of features that you ...
    (microsoft.public.publisher.webdesign)
  • Re: Website review sought - comments invited
    ... jewellery and for ease of access it is now available through ... The thumbnails load first and you get to see them. ... While you are looking at the thumbnails, the bigger images are ...
    (rec.crafts.jewelry)