• C# WinForm通过WebClient实现文件上传下载


    /////// WebClient上传文件至服务器 ///
    /// 
    文件名,全路径格式
    /// 
    服务器文件夹路径
    private void UpLoadFile(string fileNamePath,string uriString)
    {
    //string fileName = fileNamePath.Substring(fileNamePath.LastIndexOf("\\") + 1);
    NewFileName = DateTime.Now.ToString("yyMMddhhmmss") + DateTime.Now.Millisecond.ToString() + fileNamePath.Substring(fileNamePath.LastIndexOf("."));
    string fileNameExt = fileName.Substring(fileName.LastIndexOf(".") + 1);
    if(uriString.EndsWith("/") == false)
    uriString = uriString + "/"; uriString = uriString + NewFileName;
    /**//// 创建WebClient实例
    WebClient myWebClient = new WebClient();
    myWebClient.Credentials = CredentialCache.DefaultCredentials;
    // 要上传的文件
    FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
    //FileStream fs = OpenFile();
    BinaryReader r = new BinaryReader(fs);
    try {
    //使用UploadFile方法可以用下面的格式
    //myWebClient.UploadFile(uriString,"PUT",fileNamePath);
    byte[] postArray = r.ReadBytes((int)fs.Length);
    Stream postStream = myWebClient.OpenWrite(uriString,"PUT");
    if(postStream.CanWrite)
    {
    postStream.Write(postArray,0,postArray.Length);
    }
    else
    {
    MessageBox.Show("文件目前不可写!");
    } postStream.Close();
    }
    catch
    {
    MessageBox.Show("文件上传失败,请稍候重试~");
    }
    }
    /// 下载服务器文件至客户端
            /// 被下载的文件地址,绝对路径
            /// 另存放的目录
            public void Download(string URL, string Dir)
            {
                WebClient client = new WebClient();
                string fileName = URL.Substring(URL.LastIndexOf("\\") + 1);
                //被下载的文件名
                string Path = Dir+fileName;
                //另存为的绝对路径+文件名
                try
                {
                    WebRequest myre = WebRequest.Create(URL);
                }
                catch
                {
                    //MessageBox.Show(exp.Message,"Error");
                }
                try
                {
                    client.DownloadFile(URL, Path);
                }
                catch
                {
                    //MessageBox.Show(exp.Message,"Error");
                }
            } ///           
     
     /// 下载文件
    ////// 网址
    /// 
    文件名
    /// 
    进度条
    public static void DownFile( string URL, string Filename, ProgressBar Prog )
    {
    System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
    //从URL地址得到一个WEB请求
    System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
    //从WEB请求得到WEB响应
    long totalBytes = myrp.ContentLength;
    //从WEB响应得到总字节数
    Prog.Maximum = (int)totalBytes;
    //从总字节数得到进度条的最大值
    System.IO.Stream st = myrp.GetResponseStream();
    //从WEB请求创建流(读)
    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;
    //更新文件大小
    Application.DoEvents(); so.Write(by, 0, osize);
    //写流
    Prog.Value = (int)totalDownloadedByte;
    //更新进度条
    osize = st.Read(by, 0, (int)by.Length);
    //读流
    }
    //关闭流
    st.Close();
    }
  • 相关阅读:
    痞子衡嵌入式:在i.MXRT启动头FDCB里调整Flash工作频率也需同步设Dummy Cycle (以IS25WP128为例)
    《痞子衡嵌入式半月刊》 第 29 期
    痞子衡嵌入式:从头开始认识i.MXRT启动头FDCB里的lookupTable
    痞子衡嵌入式:MCUXpresso IDE下在线调试时使用不同复位策略的现象总结
    痞子衡嵌入式:关于恩智浦入驻B站的一些思考
    《痞子衡嵌入式半月刊》 第 28 期
    痞子衡嵌入式:分享一个i.MXRT系列配套DRAM压力测试上位机工具(i.MXRT DRAM Tester)
    痞子衡嵌入式:在i.MXRT1060-EVK上利用memtester程序给SDRAM做压力测试
    痞子衡嵌入式:内存读写正确性压力测试程序(memtester)
    痞子衡嵌入式:盘点国内MCU级RISC-V内核IP厂商
  • 原文地址:https://www.cnblogs.com/yc1990/p/2869879.html
Copyright © 2020-2023  润新知