文件下载(三)

不多说,用代码说明更好点

          /// <summary>
        
/// WebClient上传文件至服务器
        
/// </summary>
        
/// <param name="localFilePath">文件名,全路径格式</param>
        
/// <param name="serverFolder">服务器文件夹路径</param>
        
/// <param name="reName">是否需要修改文件名,这里默认是日期格式</param>
        
/// <returns></returns>
        public static bool UploadFile(string localFilePath, string serverFolder,bool reName)
        
{
            
string fileNameExt, newFileName, uriString;
            
if (reName)
           
{
                 fileNameExt
= localFilePath.Substring(localFilePath.LastIndexOf(".") + 1);
                 newFileName
= DateTime.Now.ToString("yyMMddhhmmss") + fileNameExt;
             }

            
else
           
{
                 newFileName
= localFilePath.Substring(localFilePath.LastIndexOf("//")+1);
             }


            
if (!serverFolder.EndsWith("/") && !serverFolder.EndsWith("//"))
            
{
                 serverFolder
= serverFolder + "/";
             }


             uriString
= serverFolder + newFileName;   //服务器保存路径
            /// 创建WebClient实例
             WebClient myWebClient = new WebClient();
             myWebClient.Credentials
= CredentialCache.DefaultCredentials;

            
// 要上传的文件
             FileStream fs = new FileStream(newFileName, FileMode.Open, FileAccess.Read);
             BinaryReader r
= new BinaryReader(fs);
            
try
           
{
                
//使用UploadFile方法可以用下面的格式
                
//myWebClient.UploadFile(uriString,"PUT",localFilePath);
                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("文件上传失败,请稍候重试~");
                return false;
             }


            
return true;
         }

           /// <summary>
        
/// 下载服务器文件至客户端
        
/// </summary>
        
/// <param name="uri">被下载的文件地址</param>
        
/// <param name="savePath">另存放的目录</param>

        public static bool Download(string uri, string savePath)
      
{
            
string fileName;  //被下载的文件名
            if (uri.IndexOf("//") > -1)
          
{
                 fileName
= uri.Substring(uri.LastIndexOf("//") + 1);
             }

            
else
           
{
                 fileName
= uri.Substring(uri.LastIndexOf("/") + 1);  
             }



            
if (!savePath.EndsWith("/") && !savePath.EndsWith("//"))
           
{
                 savePath
= savePath + "/";
             }


             savePath
+= fileName;   //另存为的绝对路径+文件名

             WebClient client
= new WebClient();
            
try
           
{
                 client.DownloadFile(uri, savePath);
             }

            
catch
          
{
                
return false;
             }


            
return true;
         }


************************************************

注意:
命名空间
System.Net;
System.IO;
上传IIS虚拟目录需要给写入权限,要不然就报 403 错误。

下载可能需要匿名访问权限。

参考:http://www.cnitblog.com/hj627/archive/2007/01/23/10939.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值