Xtremcool Xtremcool - 29 days ago 13
jQuery Question

Check URL will show Image or not c#code

How can i check that particular URL contains image and will surely show in my site

For example :

If Url path is set to this

http://meenaprints.in/Admin/images/productimg/thumbnail/436x636_4736.jpg


which will retrieving image than in return true

And suppose if image URL path is set to this

http://meenaprints.in/Admin/images/productimg/thumbnail/436x636_473123.jpg


which will not retrieving image than return false

I want this code to be written on server side c#

Instead of return true/false we will be using If/Else condition

Answer

Try this:

public bool doesImageExistRemotely(string uriToImage, string mimeType)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uriToImage);
        request.Method = "HEAD";

        try
        {
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            if (response.StatusCode == HttpStatusCode.OK && response.ContentType == mimeType)
            {
                return true;
            }
            else
            {
                return false;
            }   
        }
        catch
        {
            return false;
        }
    }