How to check if an image exists in http: //someurl/myimage.jpg in C # / ASP.NET?

How to check if an image exists http: //someurl/myimage.jpg in C # / ASP.NET There seems to be a way to check this, but I cannot find it.

I found this one , but actually it does not answer the question.

+3
source share
5 answers

This code should work:

private static bool UrlExists(string url)
{
    try
    {
        new System.Net.WebClient().DownloadData(url);
        return true;
    }
    catch (System.Net.WebException e)
    {
        if (((System.Net.HttpWebResponse)e.Response).StatusCode == System.Net.HttpStatusCode.NotFound)
            return false;
        else
            throw;
    }
}
+12
source

You can try to use System.Net.WebRequestit to send a “HEAD” request to this URL and check the response to see if it exists - this should be done without preloading.

+11

string fileextension = Path.GetExtension( );

if (fileextension.ToLower() == ".png" || fileextension.ToLower() == ".jpg" || fileextension.ToLower() == ".jpeg" || fileextension.ToLower() == ".gif" || fileextension.ToLower() == ".bmp"){}
+2

, this this . , , FileExist.

+1

System.Net.WebClient.DownloadFile, URL- , . ( , 404 )

Its the only way to do this from a URL. The System.IO namespace and all the functions there are designed for files on the local machine or on the network, so in this situation they will be useless to you.

+1
source

Source: https://habr.com/ru/post/1721336/


All Articles