1. Add a Button control to the Window form and double-click inside
2. Enter the method body and write the method
private void Btndownload_click (object sender, EventArgs e)
{
DialogResult rs = MessageBox.Show ("Are you sure you want to download the file?" "," System hint ", messageboxbuttons.yesno,messageboxicon.information);
if (rs = = Dialogresult.yes)
{
MessageBox.Show ("Downloading, please later ... ");
string URL = "http ://localhost:8088/cs_dsp.zip "; This is the server resource
string filename = @ "D:\CS_Dsp.zip"; //This download to local save path
The object that gets the client request
System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest) System.Net.HttpWebRequest.Create (URL);
Object that gets the browser response
System.Net.HttpWebResponse MYRP = (System.Net.HttpWebResponse) myrq.getresponse ();
Long totalbytes = Myrp. ContentLength;
System.IO.Stream st = Myrp. GetResponseStream ();
System.IO.Stream so = new System.IO.FileStream (filename, System.IO.FileMode.Create);
Long totaldownloadedbyte = 0;
Byte[] by = new byte[1024];
int osize = St. Read (by, 0, (int.) by. Length);
while (Osize > 0)
{
Totaldownloadedbyte = osize + totaldownloadedbyte;
So. Write (by, 0, osize);
Osize = St. Read (by, 0, (int.) by. Length);
}
So. Close ();
St. Close ();
MessageBox.Show ("Download file succeeded! ");
}
Else
{
MessageBox.Show ("Cancel the download! ");
}
}
3. Click to download the file for testing
Note: If you have a file that you write yourself at the server URL, you can download it.
C # download files from server to client source