URL Formats are not supported

I am reading File with File.OpenRead method, I am giving this path

   http://localhost:10001/MyFiles/folder/abc.png

I have tried this as well but no luck

http://localhost:10001//MyFiles//abc.png

but its giving

URL Formats are not supported

When I give physical path of my Drive like this,It works fine
d:MyFolderMyProjectMyFilesfolderabc.png

How can I give file path to an Http path?

this is my code

public FileStream GetFile(string filename)
{
    FileStream file = File.OpenRead(filename);
    return file;
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Have a look at WebClient (MSDN docs), it has many utility methods for downloading data from the web.

If you want the resource as a Stream, try:

using(WebClient webClient = new WebClient())
{
    using(Stream stream = webClient.OpenRead(uriString))
    {
        using( StreamReader sr = new StreamReader(stream) )
        {
            Console.WriteLine(sr.ReadToEnd());
        }
    }
}

Method 2

You could either use a WebClient as suggested in other answers or fetch the relative path like this:

var url = "http://localhost:10001/MyFiles/folder/abc.png";

var uri = new Uri(url);
var path = Path.GetFileName(uri.AbsolutePath);

var file = GetFile(path);
// ...

In general you should get rid of the absolute URLs.

Method 3

The best way to download the HTML is by using the WebClient class. You do this like:

    private string GetWebsiteHtml(string url)
    {
        WebRequest request = WebRequest.Create(url);
        WebResponse response = request.GetResponse();
        Stream stream = response.GetResponseStream();
        StreamReader reader = new StreamReader(stream);
        string result = reader.ReadToEnd();
        stream.Dispose();
        reader.Dispose();
        return result;
    }

Then, If you want to further process the HTML to ex. extract images or links, you will want to use technique known as HTML scrapping.

It’s currently best achieved by using the HTML Agility Pack.

Also, documentation on WebClient class: MSDN

Method 4

Here I found this snippet. Might do exactly what you need:

using(WebClient client = new WebClient()) {
   string s = client.DownloadFile(new Uri("http://.../abc.png"), filename);
}

It uses the WebClient class.

Method 5

To convert a file:// URL to a UNC file name, you should use the Uri.LocalPath property, as documented.

In other words, you can do this:

public FileStream GetFile(string url)
{
    var filename = new Uri(url).LocalPath;
    FileStream file = File.OpenRead(filename);
    return file;
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x