Click here to Skip to main content
12,945,437 members (65,194 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked
Posted 7 May 2012

How To Read a File from Web in ASP.NET

, 8 May 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Reading a file from web in ASP.NET


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;
        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

This method is useful for low-capacity files.

Goodluck !


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


About the Author

MB Seifollahi
Software Developer 1
Iran (Islamic Republic of) Iran (Islamic Republic of)
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Abolfazl Ghavidel13-May-12 2:28
memberAbolfazl Ghavidel13-May-12 2:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170518.1 | Last Updated 8 May 2012
Article Copyright 2012 by MB Seifollahi
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid