Efficient way to check if web resource/file exists?
-
Whats the best most efficient way of checking whether a resource (image, page, document etc.) exists at a particular URL web address location. Eg. I have a url http://www.example.com/myImage.jpg and I want to check whether myImage.jpg actually exists. At the moment I'm using the WebClient class like so...
WebClient client = new WebClient(); try { if(client.Download("http://www.example.com/myImage.jpg").Length > 0) { // File exists } else { // File doesn't exist } } catch { // File doesn't exist }
...problem with this method though is that it downloads the entire file to check it's existence. I'm not interested in the file, just whether the server will return a 404 or not. Cheers!Dominic Pettifer Blog: www.dominicpettifer.co.uk
-
Whats the best most efficient way of checking whether a resource (image, page, document etc.) exists at a particular URL web address location. Eg. I have a url http://www.example.com/myImage.jpg and I want to check whether myImage.jpg actually exists. At the moment I'm using the WebClient class like so...
WebClient client = new WebClient(); try { if(client.Download("http://www.example.com/myImage.jpg").Length > 0) { // File exists } else { // File doesn't exist } } catch { // File doesn't exist }
...problem with this method though is that it downloads the entire file to check it's existence. I'm not interested in the file, just whether the server will return a 404 or not. Cheers!Dominic Pettifer Blog: www.dominicpettifer.co.uk
Include System.IO and System.Text namespace to your project Then you would be able to access File class which has a method defined for checking the existence of files as shown below if (File.Exists(@"//localhost/command.gif") == false) { Response.Redirect("FileNotFound.htm"); } else { Response.Write("File Exists"); } I tested this code on my localhost, it works fine. I think this code would access the client machines path for the file and not the server's, just verify and let me know. -- modified at 9:37 Monday 25th June, 2007
-
Include System.IO and System.Text namespace to your project Then you would be able to access File class which has a method defined for checking the existence of files as shown below if (File.Exists(@"//localhost/command.gif") == false) { Response.Redirect("FileNotFound.htm"); } else { Response.Write("File Exists"); } I tested this code on my localhost, it works fine. I think this code would access the client machines path for the file and not the server's, just verify and let me know. -- modified at 9:37 Monday 25th June, 2007
gladiatron wrote:
I tested this code on my localhost, it works fine. I think this code would access the client machines path for the file and not the server's, just verify and let me know.
Just tested it with IronPython:
>>> ex = File.Exists("http://www.codeproject.com/script/images/news\_answer.gif")
ex
Falseregards
-
Whats the best most efficient way of checking whether a resource (image, page, document etc.) exists at a particular URL web address location. Eg. I have a url http://www.example.com/myImage.jpg and I want to check whether myImage.jpg actually exists. At the moment I'm using the WebClient class like so...
WebClient client = new WebClient(); try { if(client.Download("http://www.example.com/myImage.jpg").Length > 0) { // File exists } else { // File doesn't exist } } catch { // File doesn't exist }
...problem with this method though is that it downloads the entire file to check it's existence. I'm not interested in the file, just whether the server will return a 404 or not. Cheers!Dominic Pettifer Blog: www.dominicpettifer.co.uk
Your question is very vague..why do you need to write the code to check for file existence on some other web server ? Its a built in thing, if u request for a non-existent page, the logic on that server will automatically handle the request and show you an error page. However, if u wanna do this for your own server you can, using the below given code. if (File.Exists(Server.MapPath("web.gif"))) { Response.Write("File Exists"); } else { Response.Redirect("FileNotFound.htm"); } This code will map the virtual path of your server so if some client browser requests for a file that does not exist on your web server, he will get an error page other wise the page will load, now it depends on you how you customize the application for your clients.
-
Your question is very vague..why do you need to write the code to check for file existence on some other web server ? Its a built in thing, if u request for a non-existent page, the logic on that server will automatically handle the request and show you an error page. However, if u wanna do this for your own server you can, using the below given code. if (File.Exists(Server.MapPath("web.gif"))) { Response.Write("File Exists"); } else { Response.Redirect("FileNotFound.htm"); } This code will map the virtual path of your server so if some client browser requests for a file that does not exist on your web server, he will get an error page other wise the page will load, now it depends on you how you customize the application for your clients.
Long story but we have a list of external URLs which point to images on another web server, which we need to display on our site via an img tag or asp:Image control. Thing is some of these URLS are incorrect and point to images that don't exist, and rather than display an empty image box with a red X, they want a replacement image instead. Basically, they want us to guard against their own bad data. I know it's additional overhead but it's what they want, I think I can optimise it by caching the state of the external image for a time. But File.Exists("http://www.example.com/image123.jpg"); should do the trick. If you know of a more efficient way let me know. Cheers! :)
Dominic Pettifer Blog: www.dominicpettifer.co.uk