zoukankan      html  css  js  c++  java
  • C#从服务器下载文件到客户端源码

    1、在window窗体加个button控件,双击进去

    2、进入方法体中,编写方法

     private void btnDownload_Click(object sender, EventArgs e)
            {
                DialogResult rs = MessageBox.Show("是否确定下载文件?", "系统提示", MessageBoxButtons.YesNo,MessageBoxIcon.Information);
                if (rs == DialogResult.Yes)
                {
                    MessageBox.Show("正在下载,请稍后。。。。。。");
                    string URL = "http://localhost:8088/CS_Dsp.zip"; //这个是服务器资源
                    string filename = @"D:CS_Dsp.zip";    //这个下载到本地保存路径

          //得到客户端请求的对象
                    System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);

          //得到浏览器响应的对象
                    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("下载文件成功!");
                }
                else
                {
                    MessageBox.Show("取消下载!");
                }
            }

    3、点击下载文件进行测试

    注意:前提在服务器url有你自己写的文件,才能下载下来

  • 相关阅读:
    senlin __init__() got an unexpected keyword argument 'additional_headers'
    Delphi 全局画点TCanvas.Pixels[X,Y]
    sql server 列修改null 变成not null
    Delphi记录record中的变体
    delphi无边框可拖动窗体
    bootstrap相关资料
    重装windows导致grub损坏
    RabbitMQ的安装(Docker安装)
    RabbitMQ的安装以及使用(Windows环境)
    如果安装rabittmq后,输入http://localhost:15672不出页面的
  • 原文地址:https://www.cnblogs.com/xielong/p/5206911.html
Copyright © 2011-2022 走看看