Bool downloadfile (string Surl, progressbar pProgress, string filename) (File Download, progress bar)

Source: Internet
Author: User

Http://www.vbcity.com/forums/topic.asp? Tid = 41354
Hi all

I want to know how can I get the size of the file being downloaded and the progress status so I can put it in a progress bar.

Code:

Dim web as new system. net. WebClient ()
Web. downloadfile ("http://www.somethig.com", curdir () & "\ file.exe ")
 

If someone knows I wocould be very thankfull

THX in advanced ..


Edited by-useless on 9/25/2003 1:04:22

9/23/2003 2:20:01 AM
#1Re: Download Status (WebClient)  
reply W/quote B 'mark-it! edit Del
Useless

(Addicted Member)


Posts106
Since: Aug 31,200 3
From: Iceland

This reply is rated1Point.
Well after hrs of search I found the answer and here it is for you all ..

Code:

Function downloadchunks (byval Surl as string, byval pProgress as progressbar, byval filename as string)
Dim wremote as system. net. webrequest
Dim urlreq as httpwebrequest
Dim urlres as httpwebresponse
Dim filestreamer as new filestream (filename, filemode. Create)
Dim bbuffer (999) as byte
Dim ibytesread as integer

Try
Urlreq = webrequest. Create (Surl)
Urlres = urlreq. getresponse
Dim schunks as stream = urlreq. getresponse. getresponsestream
PProgress. Maximum = urlres. contentlength

Do
Ibytesread = schunks. Read (bbuffer, 0, 1000)
Filestreamer. Write (bbuffer, 0, ibytesread)
If pProgress. Value + ibytesread <= pProgress. Maximum then
PProgress. Value + = ibytesread
Else
PProgress. value = pProgress. Maximum
End if
Loop until ibytesread = 0
PProgress. value = pProgress. Maximum
Schunks. Close ()
Filestreamer. Close ()
Return sresponsedata
Catch
Msgbox (ERR. description)
End try
End Function

 

7/13/2004 12:23:21
#2Re: Download Status (WebClient)  
reply W/quote B 'mark-it! edit Del
Vbgod

(New member)


Posts: 2
Since: Jul 13,200 4
From: USA

This reply is rated3Points.

Quote:

PostedUselessOn 9/23/2003 2:20:01 am (PST ):

Well after hrs of search I found the answer and here it is for you all ..

Code:

Function downloadchunks (byval Surl as string, byval pProgress as progressbar, byval filename as string)
Dim wremote as system. net. webrequest
Dim urlreq as httpwebrequest
Dim urlres as httpwebresponse
Dim filestreamer as new filestream (filename, filemode. Create)
Dim bbuffer (999) as byte
Dim ibytesread as integer

Try
Urlreq = webrequest. Create (Surl)
Urlres = urlreq. getresponse
Dim schunks as stream = urlreq. getresponse. getresponsestream
PProgress. Maximum = urlres. contentlength

Do
Ibytesread = schunks. Read (bbuffer, 0, 1000)
Filestreamer. Write (bbuffer, 0, ibytesread)
If pProgress. Value + ibytesread <= pProgress. Maximum then
PProgress. Value + = ibytesread
Else
PProgress. value = pProgress. Maximum
End if
Loop until ibytesread = 0
PProgress. value = pProgress. Maximum
Schunks. Close ()
Filestreamer. Close ()
Return sresponsedata
Catch
Msgbox (ERR. description)
End try
End Function

 

Your VB. NET code is pure genius

It's the only one that seems to work, but... do you also know anything about C #?

I was trying to convert that VB code to C #, But I 've ran into one problem.
:-/

Herb

7/13/2004 11:39:54
#3Re: [resolved] Download Status (WebClient)  
 Reply   W/quote   B 'mark-it!   Edit   Del 
Vbgod

(New member)


Posts: 2
Since: Jul 13,200 4
From: USA

This reply is rated3Points.
It's a lot different in C #

Code:

Private bool downloadfile (string Surl, progressbar pProgress, string filename)
{
System. net. httpwebrequest urlreq;
System. net. httpwebresponse urlres;
System. Io. filestream filestreamer;
Byte [] bbuffer = new byte [999];
Int ibytesread = 0;

Try
{
Filestreamer = new filestream (filename, system. Io. filemode. Create );
Urlreq = (httpwebrequest) system. net. webrequest. Create (Surl );
Urlres = (httpwebresponse) urlreq. getresponse ();
Stream schunks = urlreq. getresponse (). getresponsestream ();
PProgress. Maximum = convert. toint32 (urlres. contentlength );
PProgress. Visible = false;

Do
{
Ibytesread = schunks. Read (bbuffer, 0, 1000 );
Filestreamer. Write (bbuffer, 0, ibytesread );

If (pProgress. Value + ibytesread <= pProgress. Maximum)
{
PProgress. Value + = ibytesread;
}
Else
{
PProgress. value = pProgress. maximum;
}
}
While (ibytesread! = 0 );

PProgress. value = pProgress. maximum;
Schunks. Close ();
Filestreamer. Close ();
Return true;
}
Catch (exception ee)
{
MessageBox. Show (EE. Message );
Return false;
}
}

3/23/2005 3:49:31 AM
#4Re: [resolved] Download Status (WebClient)  
 Reply   W/quote   B 'mark-it!   Edit   Del 
riico

( new member )


posts : 1
since : Mar 23,200 5

This reply is rated 3 points.
Hi all,
I have noticed that the script doesn't work with small files. it works only with files with a lot of megabytes.

somebody can help me to modify the code for run it also on small file? I work on C #

thanks a lot!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.