473,320 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Download file with DownloadFile method of the WebClient class

Hello.

I'm downloading some file from some http resource to file on my local disk
with DownloadFile method of the WebClient class.
Since the file is pretty large I want to show the downloading process in
progress bar.

Can anybody advice me how can I do this?

Thank you.
Nov 16 '05 #1
3 3463
Hi Mark,

I think you need to use the HttpWebRequest/Response and handle the Stream
downloading.

Read the Stream in a loop and update the progress in each pass.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
Can you show me some simple code please?

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:opsmvgmkswklbvpo@pbn_computer...
Hi Mark,

I think you need to use the HttpWebRequest/Response and handle the Stream
downloading.

Read the Stream in a loop and update the progress in each pass.

--
Happy Coding!
Morten Wennevik [C# MVP]

Nov 16 '05 #3
There is a quite good example in the documentations on

HttpWebResponse.GetResponseStream()

using that example substitute the code following the line

Stream receiveStream = myHttpWebResponse.GetResponseStream();

with something like this

byte[] data = ReadStream(receiveStream);
FileStream fs = File.Create("myfile.dat");
fs.Write(data, 0, data.Length);
fs.Close();

....

private byte[] ReadStream(Stream receiveStream)
{
byte[] buffer = new byte[4096]; // size of the buffer can be adjusted for
speed optimization

using(MemoryStream ms = new MemoryStream())
{
while(true)
{
int count = receiveStream.Read(buffer, 0, buffer.Length);

if(count <= 0) // no more to read, or possibly no feed
return ms.ToArray();

ms.Write(buffer, 0, read);
// update read status here
// unless you are doing multiple threads with invokes
// you will also want to do an Application.DoEvents()
// or nothing will appear to be updated
label1.Text = ms.Length + " bytes read";
Application.DoEvents();
}
}

PS! this code is untested and may contain typos.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: mphanke | last post by:
Hi, how can I connect to a restricted directory on a server and download a file, if there is a newer version, than the one on the local file system? Something like ...
2
by: John Spiegel | last post by:
Hi all, How does one allow a user to download a file WITH selecting where it should be downloaded to on their machine? Also, when using the WebClient.Download method, will the destination path...
16
by: Bob Moore | last post by:
We have a requirement to put a client onto machines intended for some telephony work. The database access will be via ASP.Net pages, and that's fine, but the telephony stuff requires access to some...
11
by: belgie | last post by:
How can I retrieve an image (.jpg) from a web site using http with VB dotnet? thanks Bill
1
by: g1ga | last post by:
as in the object, i'm developing a simple application that download a file from the web through the method webclient.downloadfile. all works fine. now i would like to add a progressbar control to...
8
by: ad | last post by:
How can I download a file form Server to client?
1
by: apondu | last post by:
Hi, I am new to web services and i am facing a problem. I am interested in downloading some file from internet and use it for furthur processing For eg. i have a file at a the following URL and...
0
by: apondu | last post by:
Hi, I am new to web services and i am facing a problem. I am interested in downloading some file from internet and use it for furthur processing For eg. i have a file at a the following URL and...
0
by: swheat | last post by:
Hello I would like to be able to automate downloading a file from a website which requires that I submit form parameters to start the download. The problem I am having is described exactly in this...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.