2011-11-15 37 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