Saturday, 22 December 2012 14:36

how to download and upload files by c#

Written by 
Rate this item
(0 votes)

System.Net namespace is for dealing with networking.It
is concerned with operations like downloading, uploading
files,making requests by http protocols.

if you want to do a simple task like requesting a file
from a URL, then you will find easiest to use the class
system.Net.WebClient of .NET.following are the
ways to download and upload by this class -

Download files::
we will consider two ways for downloading a file using
WebClient.one is saving the file and the other is
processing the contents directly in our application.
to save the file we must call the DownloadFile() method,
which takes two values as parameters,the URL(where we
want to retrieve file),and the file name.the code shown
below-

WebClient TheClient = new WebClient ();
TheClient.DownloadFile("http://www.watchmovizonline.
blogspot.com/index.aspx", " index.aspx");

the following codes shows how we can show the
contents of the data downloaded in the list box using
WebClient.OpenRead().

public mainform(){
InitializeComponent();
System.Net.WebClient TheClient = new WebClient();
Stream stream = TheClient.OpenRead("http://www.
Watchmovizonline.blogspot.com");
StreamReader sreader = new StreamReader(stream);
string data;
do{
data = sreader.ReadLine();
theListBox.Items.Add(data);}
while (data !=null);
stream.Close();}

Upload files::
in this case we will simply use the following two methods
UploadFile() and UploadData().the UPloadFile() uploads a
given file,but the UploadData() uploads binary data provided
by an array of objects.the codes follows-

WebClient TheClient = new WebClient();
TheClient.UploadFile("http://www.Watchmovizonline.blogspot
.com/files/index.aspx", "d:\sitefiles\file.aspx");

byte [] image1;

//code for initialising image so that it contains all binary
data for some jpg files
client.UploadData("http://www.watchmovizonline.blogspot.com/
files/images/logo.jpg", image1);

so in this way we can download and upload data from or to a
website using c#.if you want to know more then google it.

Read 1280 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.
Login to post comments