C# WinForm 文件上传下载

本文介绍了如何使用C#中的WebClient类实现文件的上传与下载功能。具体包括:通过PUT方法上传本地文件到服务器,并从服务器下载文件到客户端的过程。
ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
InBlock.gif  
/// WebClient上传文件至服务器
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="fileNamePath">文件名,全路径格式</param>
ExpandedBlockEnd.gif  
/// <param name="uriString">服务器文件夹路径</param>

None.gif  private void UpLoadFile(string fileNamePath,string uriString)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif{
InBlock.gif   
//string fileName = fileNamePath.Substring(fileNamePath.LastIndexOf("\\") + 1);
InBlock.gif
   NewFileName = DateTime.Now.ToString("yyMMddhhmmss"+ DateTime.Now.Millisecond.ToString() + fileNamePath.Substring(fileNamePath.LastIndexOf("."));
InBlock.gif
InBlock.gif   
string fileNameExt = fileName.Substring(fileName.LastIndexOf("."+ 1);
InBlock.gif   
if(uriString.EndsWith("/"== false) uriString = uriString + "/";
InBlock.gif
InBlock.gif   uriString 
= uriString + NewFileName;
ExpandedSubBlockStart.gifContractedSubBlock.gif   
/**//// 创建WebClient实例
InBlock.gif   WebClient myWebClient = new WebClient();
InBlock.gif   myWebClient.Credentials 
= CredentialCache.DefaultCredentials;
InBlock.gif
InBlock.gif   
// 要上传的文件
InBlock.gif
   FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
InBlock.gif   
//FileStream fs = OpenFile();
InBlock.gif
   BinaryReader r = new BinaryReader(fs);
InBlock.gif   
try
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
//使用UploadFile方法可以用下面的格式
InBlock.gif    
//myWebClient.UploadFile(uriString,"PUT",fileNamePath);
InBlock.gif
    byte[] postArray = r.ReadBytes((int)fs.Length);
InBlock.gif    Stream postStream 
= myWebClient.OpenWrite(uriString,"PUT");
InBlock.gif    
if(postStream.CanWrite)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     postStream.Write(postArray,
0,postArray.Length);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     MessageBox.Show(
"文件目前不可写!");
ExpandedSubBlockEnd.gif    }

InBlock.gif    postStream.Close();
ExpandedSubBlockEnd.gif   }

InBlock.gif   
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    MessageBox.Show(
"文件上传失败,请稍候重试~");
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif  }

None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif  
/**//// <summary>
InBlock.gif  
/// 下载服务器文件至客户端
InBlock.gif
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="URL">被下载的文件地址,绝对路径</param>
ExpandedBlockEnd.gif  
/// <param name="Dir">另存放的目录</param>

None.gif  public void Download(string URL,string Dir)
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif{
InBlock.gif   WebClient client 
= new WebClient();
InBlock.gif   
string fileName = URL.Substring(URL.LastIndexOf("\\"+ 1);  //被下载的文件名
InBlock.gif

InBlock.gif   
string Path = Dir+fileName;   //另存为的绝对路径+文件名
InBlock.gif
   
InBlock.gif   
try
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    WebRequest myre
=WebRequest.Create(URL); 
ExpandedSubBlockEnd.gif   }

InBlock.gif   
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
//MessageBox.Show(exp.Message,"Error"); 
ExpandedSubBlockEnd.gif
   }

InBlock.gif   
InBlock.gif   
try
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    client.DownloadFile(URL,Path);
InBlock.gif
ExpandedSubBlockEnd.gif   }
 
InBlock.gif   
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
//MessageBox.Show(exp.Message,"Error");
ExpandedSubBlockEnd.gif
   }
 
ExpandedBlockEnd.gif  }
 
None.gif
None.gif

转载于:https://www.cnblogs.com/hzuIT/articles/766307.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值