C # download files from server to client source

Source: Internet
Author: User

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

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.