3

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:\MyFolder\MyProject\MyFiles\folder\abc.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;
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Syed Salman Raza Zaidi
  • 2,172
  • 9
  • 42
  • 87
  • This might be your answer: http://stackoverflow.com/questions/1048199/easiest-way-to-read-from-a-url-into-a-string-in-net – Bart Friederichs Jul 29 '13 at 07:42
  • use Server.MapPath("//MyFiles")+"//abc.png" as filename – Annie Jul 29 '13 at 07:44
  • you need to use Server.MapPath() as file operation required physical file path not virtual path. in your case you should use string filePath=Server.MapPath("~/MyFiles/folder/abc.png"); – Rajesh Kumar Jul 29 '13 at 07:44

5 Answers5

9

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());
        }
    }
}
Ross McNab
  • 11,337
  • 3
  • 36
  • 34
3

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.

philipproplesch
  • 2,127
  • 17
  • 20
2

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

Kamil Solecki
  • 1,106
  • 20
  • 34
1

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.

Community
  • 1
  • 1
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
0

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;
}
Dave Van den Eynde
  • 17,020
  • 7
  • 59
  • 90