Implemented download with progress

Source: Internet
Author: User

I remember that when I was just using it, webclient did look quite simple, but after implementation, the download of small files was complete.

Large files must be downloaded all the time.

Later, I found it and used WebRequest and WebResponse to download the file with a progress prompt ..
The following code is extracted from one of my software. Only the key part is described...
========================================================== ==================================
If (Downloading = false) // if no file is being downloaded
{
TempDown = CurrentFileName;
If (CurrentFileName = "") {TempDown = B ;}
WhichDown = 1;
System. Threading. Thread ApcThread2 = new System. Threading. Thread (new System. Threading. ThreadStart (DownFile ));
ApcThread2.Start ();

}
Else
{
MessageBox. Show ("sorry, the file is being downloaded.", "prompt", messageboxbuttons. OK, messageboxicon. information );
}
##################
In general, if no file is being downloaded, start a new thread... download the file... the code of the downfile function ....
In simple cases, no comments will be made.
========================================================== ============================================
// Download block #################################### ######################################## #########################
Private void downfile ()
{
If (tempdown! = "")
{

If (downloading = false) // if no file is downloaded
{
Long fb;
This. apc_status_1.Text = "connecting to" + TempDown;
Downloading = true;
Try
{
// ===== Try URL validity and initialize the download Interface
WebRequest myre = WebRequest. Create (TempDown );
WebResponse mwrite = myre. GetResponse ();
Fb = mwrite. ContentLength;
This. apc_status_1.Text = "connection successful... download started ..";
Pbar. Value = 0;
Pbar. Maximum = (int) fb;
Pbar. Visible = true;
This. ApcList. Height = 156;
// ==== Start download
WebClient wc = new WebClient ();
SaveFileDialog sf = new SaveFileDialog ();
Sf. Title = "select the file storage location ";
Filename = CurrentFileName;
Sf. FileName = filename. Substring (filename. LastIndexOf ("/") + 1, filename. Length-filename.LastIndexOf ("/")-1 );
Sf. ShowDialog (this );
Filename = sf. FileName;
If (filename! = "")
{
Stream srm = wc. OpenRead (TempDown );
StreamReader srmer = new StreamReader (srm );
Byte [] mbyte = new byte [fb];
Int allbyte = (int) mbyte. Length;
Int startbyte = 0;
While (fb> 0) // ################ read the file cyclically and display the progress .....
{
Application. DoEvents ();
Int m = srm. Read (mbyte, startbyte, allbyte );
If (m = 0) {break ;}
Startbyte + = m;
Allbyte-= m;
Pbar. Value + = m;
Int a1 = (int) startbyte/1024;
Int a2 = (int) fb/1024;
This. apc_status_1.Text = "connection successful .. start to download .. "+ a1.ToString () +"/"+ a2.ToString () +" KB "; // startbyte +"/"+ fb. toString ();
}

FileStream fs = new FileStream (filename, FileMode. OpenOrCreate );
Fs. Write (mbyte, 0, mbyte. Length );
Fs. Flush ();

Srm. Close ();
Srmer. Close ();
Fs. Close ();

This. apclist. Height = 170;
Pbar. Visible = false;
This. apc_status_1.text = "the file has been downloaded! ";
}

}
Catch (webexception exp) // if the address is invalid or the file cannot be found
{
MessageBox. Show (exp. message, "", messageboxbuttons. OK, messageboxicon. information );
}
Downloading = false;
}
Else
{
MessageBox. Show ("sorry, the file is being downloaded.", "", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
}
Else
{
If (WhichDown = 1)
{
MessageBox. Show ("no file is currently played.", "", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
Else
{
MessageBox. Show ("select the file you want to download from the list.", "", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
}

}//

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.