0

I have a products sale module in which products are uploaded from cj and saved in to database..today i noticed few records contained image url but returns 404(eg image url:http://www.bridalfashionmall.com/images/satin-2.jpg) hence shows no image in the repeater ..how can i check whether the url called dynamically has image in it

Karthik
  • 2,391
  • 6
  • 34
  • 65
  • http://stackoverflow.com/questions/1460273/how-to-check-if-a-file-exits-on-an-webserver-by-its-url – Niranjan Singh Nov 15 '11 at 09:29
  • @NiranjanKala : throws an exception if image does not exists.. – Karthik Nov 15 '11 at 09:39
  • Then you catch that exception... But really, you should _not_ hotlink images in a web shop. – CodeCaster Nov 15 '11 at 09:39
  • @CodeCaster: but those images are provided by the vendors of the product – Karthik Nov 15 '11 at 09:44
  • 1
    And those vendors never redesign their sites and change their URI's, causing your images to fail? And they agree to you stealing their bandwidth? And they'll never swap an image for a goatse? You should really download those images to your server and host them yourself. – CodeCaster Nov 15 '11 at 09:46
  • @CodeCaster: thanks for your help will look in to it – Karthik Nov 15 '11 at 09:52

2 Answers2

0

The method suggested by sean could be used as first pass. As second pass you can try loading the stream into image and see if it is actually image?

 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(imageFilePath);
 request.Timeout = 5000;
 request.ReadWriteTimeout = 20000;

 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
 System.Drawing.Image img = System.Drawing.Image.FromStream(response.GetResponseStream());
 // Save the response to the output stream
 Response.ContentType = "image/gif";
hungryMind
  • 6,931
  • 4
  • 29
  • 45
  • 2
    _"The method suggested by sean could be used as first pass."_ No it can't. Sites like imageshack use .jpg as links to HTML pages, and on the contrary, tons of images are hosted without extension (`http://site/images/{md5(filename)}` for example). – CodeCaster Nov 15 '11 at 09:37
  • Right, but here we assume file url are with valid file extension. – hungryMind Nov 15 '11 at 10:27
  • @hungryMind: very true i just need to check whether a image is present in that specified location – Karthik Nov 15 '11 at 10:33
  • @hungryMind don't you know what they say about assumptions? – CodeCaster Nov 15 '11 at 10:40
0
this one helped---
http://stackoverflow.com/questions/1639878/how-can-i-check-if-an-image-exists-at-http-someurl-myimage-jpg-in-c-asp-net


this one too worked

               try
                {
                    WebClient client = new WebClient();
                    client.DownloadData(ImageUrl);

                }
                catch
                {
                    imgPhoto.ImageUrl = ../User/Images/ResourceImages/Candychocolate1.jpg";//default image path
                }
Karthik
  • 2,391
  • 6
  • 34
  • 65