查找动态调用的url是否包含图像

时间:2011-11-15 09:23:13

标签: c# asp.net

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

2 个答案:

答案 0 :(得分:0)

肖恩建议的方法可以作为第一遍。作为第二遍,你可以尝试将流加载到图像中,看看它是否真的是图像?

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

答案 1 :(得分: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
                }