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

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

the following codes shows how we can show the
contents of the data downloaded in the list box using

public mainform(){
System.Net.WebClient TheClient = new WebClient();
Stream stream = TheClient.OpenRead("http://www.");
StreamReader sreader = new StreamReader(stream);
string data;
data = sreader.ReadLine();
while (data !=null);

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();
.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
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 3324 times
Super User

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

Latest discussions

  • No posts to display.