Sunteți pe pagina 1din 4

10,500,000 members 70,406 online

articles

quick answers

jokercracker

discussions

community

help

211 Sign out

Searchforarticles,questions,tips

Articles Web Development ASP.NET Howto

Download File Using C#


tejasbhalani, 30 May 2012

CPOL

169.7K

13

Rate:

4.53 15 votes
Download file using C#.

Introduction
This article describes code to download a file using C#...

Background
While browsing forums today I came across a question which asked for a solution to download a file from a web server programmatically. The solution is very simple and
below is the code which achieves the goal. Here I am downloading a file asynchronously on Button Click.

Using the code


Hide Copy Code

privatevoidbuttonDownloadFile_Click(objectsender,EventArgse)
{
stringurl=@"http://www.thereforesystems.com/wpcontent/uploads/2008/08/image35.png";
//CreateaninstanceofWebClient
WebClientclient=newWebClient();
//HookupDownloadFileCompletedEvent
client.DownloadFileCompleted+=newAsyncCompletedEventHandler(client_DownloadFileCompleted);
//Startthedownloadandcopythefiletoc:\temp
client.DownloadFileAsync(newUri(url),@"c:\temp\image35.png");
}
voidclient_DownloadFileCompleted(objectsender,AsyncCompletedEventArgse)
{
MessageBox.Show("Filedownloaded");
}
You can also download the file synchronously using WebClient.DownloadFile() method.

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

Share
EMAIL

TWITTER

About the Author

You may also be interested in...

File Download in ASP.NET and Tracking the


Status of Success/Failure of Download

I Cant Believe Its Not Multisite!

Resume Support for File Downloads

Red Hat JBoss BPM Suite 6 compared to Appian


7 BPM Suite

File List Downloader

Red Hat JBoss BPM Suite 6 Compared to


Pegasystems Pega 7 BPM

Comments and Discussions

Add a Comment or Question

Search Comments
Profile popupsSpacing Relaxed

Layout Normal

Go
Per page 25

Update

First Prev Next

Download files from HTTPS

MohamedImtiyaz.net

Download file from secure folder

jayprakashjaysun

My vote of 1

Member 10795245

29Sep14 18:54

regarding youtube

aman bhardwaj

10Sep14 13:51

My vote of 5

Chan Nyein

10Feb15 19:25
2Dec14 17:17

13Apr14 8:54

Like it

Siva Hyderabad

hello

Rahim Lotfi

20Feb14 2:14

Download multiple files to a folder from a url

mehrarohit

13Feb14 10:20

My vote of 4

ali yeganeh

1Nov12 1:51

Member 9319962

5Feb13 22:44

Re: My vote of 4
My vote of 4

kishore doni

Last Visit: 20Sep15 13:38 Last Update: 23Sep15 20:38


General

News

28Feb14 10:17

Suggestion

Question

28Aug12 16:57

Refresh
Bug

Answer

Joke

Rant

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
Web01 | 2.8.1509020.2 | Last Updated 30 May 2012

Select Language

Layout: fixed |
fluid

Article Copyright 2012 by tejasbhalani


Everything else Copyright CodeProject, 19992015

S-ar putea să vă placă și