Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to check file exists using url, normally we have to use file exists controller on server like
File.Exists()  its working but url path like http://localhost:8081/assets/photos/test.jpg is not working. Please anyone explain ASAP


What I have tried:

var url = "http://www.domain.com/image.png";
HttpWebResponse response = null;
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "HEAD";


try
{
    response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
    /* A WebException will be thrown if the status of the response is not `200 OK` */
}
finally
{
    // Don't forget to close your response.
    if (response != null)
    {
        response.Close();
    }
}
Posted
Updated 15-Dec-17 5:32am
Comments
Richard MacCutchan 14-Dec-17 3:56am    
What happens when you run this code?
Sinisa Hajnal 15-Dec-17 8:41am    
So...what is the problem?

1 solution

If the url is within your webserver then use Server.MapPath(url) to get the actual file location and then you can use File.Exists(). Note, the user running your app pool will need proper permissions to do so. It won't by default.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900