2011-11-15 25 views
0

我有一个产品销售模块,其中产品从cj上传并保存到数据库..今天我注意到一些包含图像url的记录,但返回404(例如图像url:http ://www.bridalfashionmall.com/images/satin-2.jpg)因此在转发器中没有显示图像..我怎么能检查动态调用的url是否有图像查找动态调用的url是否有图像

+0

http://stackoverflow.com/questions/1460273/how-to-check-if-a-file-exits-on-an-webserver-by-its-url –

+0

@NiranjanKala:如果图像不存在,则会引发异常。 – Karthik

+0

然后您会发现该异常...但是,实际上,您应该在网店中不要使用热链接图像。 – CodeCaster

回答

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 
       } 
0

sean建议的方法可能是用作第一关。作为第二遍,你可以尝试加载流到图像,看看它是否实际上是图像?

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"; 
+2

_“sean建议的方法可以用作第一遍。”_不,它不能。像imageshack这样的站点使用.jpg作为HTML页面的链接,相反,大量的图像没有扩展名(例如'http:// site/images/{md5(filename)}'。 – CodeCaster

+0

对,但在这里我们假设文件的URL是有效的文件扩展名。 – hungryMind

+0

@hungryMind:非常真实我只需要检查图像是否存在于指定的位置 – Karthik