Download And Upload A File Using C#
http://www.csharpfriends.com
World's Greatest C# Community    
Home Articles C# Forums Books C# Syntax C# Spec C# Jobs free Source Code Advertise About
 

Control Panel

[ Sign In / register ]
Points   
Notes 
My Forums
My Tutorials
My Profile

Resources

Learn
 Articles
 QuickStarts
 C# Spec
 Whitepapers
 Tools
 Class Browser
 C# Code Generator
 Links
 Misc Rss Feeds
 Code Highlight
 411 Directory
 FREE magazines
 freevb.net

Reviews
  ASP.NET Hosting

Source Code
 Get Version 1.0



C# Consulting
AspDotNetStoreFront
Chapter:   UnCategorized
Current Lesson:
Download And Upload A File Using C#
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  Windows Management Instrumentation (WMI) Tutorial [next Lesson]  Instance Constructor vs. The Class Constructor
Download And Upload A File Using C#
  by: narendra

Download And Upload A File Using C#

by: narendra

In this tutorial you will know how to download a file from a web site and upload a file to a web site through c# project.

The System.Net namespace contain most of the .NET base class that deal with networking from perspective of the client. The System.Net namespace is generally concerned with Higher-level operations for example download and upload files and making requests using the HTTP and other protocols.

The WebClient Class

If you want to do is carry out a fairly simple operation such as requesting a file from a particular URL, Then you will probably find easiest .NET class to use is System.Net.WebClient. This class is extremely high-level class Designed to perform basic operations with only one or two commands

Downloading Fiels:

There are two ways of downloading a file from a web site using WebClient, depending on whether we want to save the file, or process the contents of the directly within your application. If we simply want to save the file then we should call the DownloadFile() method takes two parameters, The URL from where we want to retrieve the file, and the file name ( or path) that we want to save the file to.
WebClient Client = new WebClient ();
Client.DownloadFile("http://www.csharpfriends.com/Members/index.aspx", " index.aspx");
More commonly, your application will want to process the data retrieved from the web site. In order to do this, you use the OpenRead () method, Which returns a stream reference. You can then simply retrieve the data from the stream.
WebClient Client = new WebClient ();
Stream strm = Client.OpenRead ("http://www.csharpfriends.com/Members/index.aspx");
The following code will demonstrate the WebClient.OpenRead () method.

In this case we will simply display the contents of the downloaded data in list box.

We create the project as standard Windows C# application, and a list box called listbox1, in which we will display the contents of the downloaded file. We make the following changes to the constructor of the main form.
public form1()
{
    InitializeComponent();

    System.Net.WebClient Client = new WebClient();
    Stream strm = Client.OpenRead("http://www.csharpfriends.com");
    StreamReader sr = new StreamReader(strm);
    string line;
    do
    {
        line = sr.ReadLine();
        listbox1.Items.Add(line);
    }
    while (line !=null);
    strm.Close();
}
Updating Files

The webClient class also features UploadFile() and UploadData() methods. The diffrence between them is that UploadFile() uploads a specified file given the file name, While UploadData() uploads binary data, which is supplied as an array of bytes:
WebClient  Client = new WebClient();
Client.UploadFile("http://www.csharpfriends.com/Members/index.aspx", 
     "c:\wesiteFiles\newfile.aspx");

byte [] image;

//code to initialise image so it contains all the binary data for some jpg file
client.UploadData("http://www.csharpfriends.com/Members/images/logocc.jpg", image);
In this way we can download and upload a file through c# project Try to upload files to yours web sites and download files.

I think now you all understand how to upload and download files using c#.

By: Narendra


1 


Build Your Own ASP.NET Website Using C# & VB.NET

Chapter:  UnCategorized
Current Lesson:
Download And Upload A File Using C#
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  Windows Management Instrumentation (WMI) Tutorial [next Lesson]  Instance Constructor vs. The Class Constructor



Today's Top Movers
vulpes 6800
MadHatter 2220
jal 867
Jeff1203 857
muster 791

Yesterday Top Movers
shakti sin.. 9
MadHatter 3
Al_Pennywo.. 2
C#fanatic 2
snaso 1



Monthly Leaders
vulpes 6800
MadHatter 2260
jal 867
Jeff1203 857
muster 791

Top Members
mosessaur 18457
Rincewind 7074
stanleytan 6995
vulpes 6800
Gsuttie 6046

Great Offers
.net hosting
Go To My Pc
Remote Pc Control
zonealarm
spam blocker
web hosting directory
ad server   C#
snadtech GoToMyPc

Top of Page

Advertise | About | Link To Us | Privacy Notice Copyright © 2003 - 2005 CSharpFriends.com  All Rights Reserved  Visual C# Developer Center