Hi Sudar,
Check this example. Now please take its reference and correct your code.
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
bool fileExist = URLExists("https://i.imgur.com/EZTq4Ql.jpg");
if (fileExist)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('File Exist')", true);
}
else
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('File Not Exist')", true);
}
}
public bool URLExists(string url)
{
System.Net.WebRequest webRequest = System.Net.WebRequest.Create(url);
webRequest.Method = "HEAD";
try
{
using (System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)webRequest.GetResponse())
{
if (response.StatusCode.ToString() == "OK")
{
return true;
}
return false;
}
}
catch
{
return false;
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim fileExist As Boolean = URLExists("https://i.imgur.com/EZTq4Ql.jpg")
If fileExist Then
ClientScript.RegisterClientScriptBlock(Me.GetType, "", "alert('File Exist')", True)
Else
ClientScript.RegisterClientScriptBlock(Me.GetType, "", "alert('File Not Exist')", True)
End If
End Sub
Public Function URLExists(ByVal url As String) As Boolean
Dim webRequest As System.Net.WebRequest = System.Net.WebRequest.Create(url)
webRequest.Method = "HEAD"
Try
Dim response As System.Net.HttpWebResponse = CType(webRequest.GetResponse, System.Net.HttpWebResponse)
If (response.StatusCode.ToString = "OK") Then
Return True
End If
Return False
Catch
Return False
End Try
End Function