分享

C# WinForm通过WebClient实现文件上传下载 (附源码)

 caoyc1989 2014-05-06

[c-sharp] view plaincopy
  1. //// <summary>   
  2. /// WebClient上传文件至服务器   
  3. /// </summary>   
  4. /// <param name="fileNamePath">文件名,全路径格式</param>   
  5. /// <param name="uriString">服务器文件夹路径</param>   
  6. private void UpLoadFile(string fileNamePath,string uriString)  
  7. {  
  8.    //string fileName = fileNamePath.Substring(fileNamePath.LastIndexOf("//") + 1);   
  9.    NewFileName = DateTime.Now.ToString("yyMMddhhmmss") + DateTime.Now.Millisecond.ToString() + fileNamePath.Substring(fileNamePath.LastIndexOf("."));  
  10.   
  11.    string fileNameExt = fileName.Substring(fileName.LastIndexOf(".") + 1);  
  12.    if(uriString.EndsWith("/") == false) uriString = uriString + "/";  
  13.   
  14.    uriString = uriString + NewFileName;  
  15.    /**//// 创建WebClient实例  
  16.    WebClient myWebClient = new WebClient();  
  17.    myWebClient.Credentials = CredentialCache.DefaultCredentials;  
  18.   
  19.    // 要上传的文件   
  20.    FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);  
  21.    //FileStream fs = OpenFile();   
  22.    BinaryReader r = new BinaryReader(fs);  
  23.    try  
  24.    {  
  25.     //使用UploadFile方法可以用下面的格式   
  26.     //myWebClient.UploadFile(uriString,"PUT",fileNamePath);   
  27.     byte[] postArray = r.ReadBytes((int)fs.Length);  
  28.     Stream postStream = myWebClient.OpenWrite(uriString,"PUT");  
  29.     if(postStream.CanWrite)  
  30.     {  
  31.      postStream.Write(postArray,0,postArray.Length);  
  32.     }  
  33.     else  
  34.     {  
  35.      MessageBox.Show("文件目前不可写!");  
  36.     }  
  37.     postStream.Close();  
  38.    }  
  39.    catch  
  40.    {  
  41.     MessageBox.Show("文件上传失败,请稍候重试~");  
  42.    }  
  43. }  
  44.   
  45.   
  46. /**//// <summary>  
  47. /// 下载服务器文件至客户端   
  48.   
  49. /// </summary>   
  50. /// <param name="URL">被下载的文件地址,绝对路径</param>   
  51. /// <param name="Dir">另存放的目录</param>   
  52. public void Download(string URL,string Dir)  
  53. {  
  54.    WebClient client = new WebClient();  
  55.    string fileName = URL.Substring(URL.LastIndexOf("//") + 1); //被下载的文件名  
  56.   
  57.    string Path = Dir+fileName;   //另存为的绝对路径+文件名   
  58.     
  59.    try  
  60.    {  
  61.     WebRequest myre=WebRequest.Create(URL);  
  62.    }  
  63.    catch  
  64.    {  
  65.     //MessageBox.Show(exp.Message,"Error");   
  66.    }  
  67.     
  68.    try  
  69.    {  
  70.     client.DownloadFile(URL,Path);  
  71.   
  72.    }  
  73.    catch  
  74.    {  
  75.     //MessageBox.Show(exp.Message,"Error");   
  76.    }  
  77. }  

 

下载带进度条代码

[c-sharp] view plaincopy
  1. /// <summary>      
  2. /// 下载文件      
  3. /// </summary>      
  4. /// <param name="URL">网址</param>      
  5. /// <param name="Filename">文件名</param>      
  6. /// <param name="Prog">进度条</param>      
  7. public static void DownFile( string URL, string Filename, ProgressBar Prog )     
  8. {     
  9. System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL); //从URL地址得到一个WEB请求      
  10. System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse(); //从WEB请求得到WEB响应      
  11. long totalBytes = myrp.ContentLength; //从WEB响应得到总字节数      
  12. Prog.Maximum = (int)totalBytes; //从总字节数得到进度条的最大值      
  13. System.IO.Stream st = myrp.GetResponseStream(); //从WEB请求创建流(读)      
  14. System.IO.Stream so = new System.IO.FileStream(Filename, System.IO.FileMode.Create); //创建文件流(写)      
  15. long totalDownloadedByte = 0; //下载文件大小      
  16. byte[] by = new byte[1024];     
  17. int osize = st.Read(by, 0, (int)by.Length); //读流      
  18. while (osize > 0)     
  19. {     
  20. totalDownloadedByte = osize + totalDownloadedByte; //更新文件大小      
  21. Application.DoEvents();     
  22. so.Write(by, 0, osize); //写流      
  23. Prog.Value = (int)totalDownloadedByte; //更新进度条      
  24. osize = st.Read(by, 0, (int)by.Length); //读流      
  25. }     
  26. so.Close(); //关闭流      
  27. st.Close(); //关闭流      
  28. }    

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多