Check if an image is available on a remote server
I have a URL variable that contains the adress of an image on a remote
server (i.e. myVar="http://www.imageserver.com/myImage.jpg");
I do a response.write("<img src='+myVar+'>")
I want to check if the image is really on the remote server to avoid displaying the "red cross" image.
Any idea ?
you can use HttpWebRequest and HttpWebResponse classes to check existence of image on remote server, following code will show how you can do that. Run sample code<%@ Page Language="C#" %>
<%@ import Namespace="System.Net" %>
<%@ import Namespace="System.IO" %>
<%@ import Namespace="System.Text" %>
<script runat="server">
void CheckImage(object sender, EventArgs e){
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(text1.Text);
try {
HttpWebResponse httpWebResponse = (HttpWebResponse)httpRequest.GetResponse();
ResponseResult.Text = String.Format("<img src='{0}' />", text1.Text);
} catch(Exception ex) {
ResponseResult.Text = ex.Message;
}
}
</script>
<html>
<head>
<title>Check remote image</title>
</head>
<body>
<form id="Form1" runat="Server">
<asp:TextBox runat="Server" id="text1" Columns="60" Text="http://www.jigar.net/quote.ashx?width=150" />
<p />
<asp:button ID="Button1" runat="Server" onclick="CheckImage" Text="Check"/>
<p />
<asp:Literal runat="Server" id="ResponseResult" />
</form>
</body>
</html>
|
 My name is Jigar Desai I share my ideas on this site and you can contact me by filling contact form.
|