Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How To Read a File from Web in ASP.NET

0.00/5 (No votes)
8 May 2012 1  
Reading a file from web in ASP.NET

Introduction

I wanted to read an HTML file to send as e-mail body in web... but I could not use FileInfo (I got an exception about URI format).

So... to do it, I use the below code to read content of file.

Using the Code

You should use System.Net and System.Text namespaces:

private string Readfile(string WebFilePath)
{
    string body = string.Empty;
    try
    {
        System.Net.WebClient wc = new System.Net.WebClient();
        byte[] raw = wc.DownloadData(WebFilePath);
        body = System.Text.Encoding.UTF8.GetString(raw);
        return body;
    }
    catch (Exception ex)
    {
        return null;
    }
} 

Points of Interest

The WebFilePath is the full path of file (and Accessible) in web... like http://MyDomain.com/Folder/file.xml.

This method is useful for low-capacity files.

Goodluck !

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here