See someone with WebClient to download, send a WebRequest to achieve progress with the download of the bar.

Source: Internet
Author: User
Tags thread tostring
client|request|web| Download
Remember used to just when, WebClient really looked very simple, but to achieve, small file is a moment on the download.

Large files must always be downloaded before they can be completed.

Later looked for, with WebRequest combination WebResponse can achieve progress prompts, download files.
Here's the code ... It was extracted from one of my software. Only the key part of the description ...
=====================================================================
if (downloading==false)//If no files are 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 currently being downloaded.") "," hint ", messageboxbuttons.ok,messageboxicon.information);
}
##################
Presumably, if no files are currently being downloaded, start a new thread .... Download file ... Here is the code for the Downfile function ....
A simple place will not be annotated.
=============================================================================
Download Block #####################################################################################################
private void Downfile ()
{
if (tempdown!= "")
{

if (downloading==false)//If no file download
{
Long FB;
This.apc_status_1.text= "is connected to" + Tempdown;
Downloading=true;
Try
{
= = Try URL validity, and initialize download interface
WebRequest myre=webrequest.create (Tempdown);
WebResponse Mwrite=myre. GetResponse ();
Fb=mwrite. ContentLength;
This.apc_status_1.text= "Connection succeeded ... Start download ... ";
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= "Please select the location where the file is stored";
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)//################ reads the file and displays 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 succeeded ... Start 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= "File Download complete!" ";
}


}
catch (WebException exp)//If the address is invalid or the file cannot be found
{
MessageBox.Show (exp. Message, "Listen to snap Tips", messageboxbuttons.ok,messageboxicon.information);
}
Downloading=false;
}
Else
{
MessageBox.Show ("Sorry, the file is currently being downloaded.") "," Listen to snap tips, messageboxbuttons.ok,messageboxicon.information);
}
}
Else
{
if (whichdown==1)
{
MessageBox.Show ("No files are currently playing.) "," Listen to snap tips, messageboxbuttons.ok,messageboxicon.information);
}
Else
{
MessageBox.Show ("Please select the file you want to download in the list.") "," Listen to snap tips, messageboxbuttons.ok,messageboxicon.information);
}
}

}//Download Block #####################################################################################################



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.