zoukankan      html  css  js  c++  java
  • 上传下载资源

    1. WebClient:提供用于将数据发送到由 URI 标识的资源及从这样的资源接收数据的常用方法。
       1         /// <summary>  
       2         /// WebClient上传文件至服务器(不带进度条) 
       3         /// </summary>  
       4         /// <param name="localFilePath">要上传的文件(全路径格式)</param>  
       5         /// <param name="targetFolder">Web服务器文件夹路径</param> 
       6         /// <returns>True/False是否上传成功</returns>  
       7         public static void UpLoadFile(string localFilePath, string targetFolder)
       8         {
       9             //get file name
      10             string fileName = Path.GetFileName(localFilePath);
      11             if (string.IsNullOrEmpty(fileName))
      12             {
      13                 throw new InvalidOperationException("the file name is empty");
      14             }
      15             //get target url
      16             string targetUrl = Path.Combine(targetFolder, fileName);
      17 
      18             WebClient webClient = null;
      19             FileStream myFileStream = null;
      20             BinaryReader myBinaryReader = null;
      21             Stream writeStream = null;
      22 
      23             try
      24             {
      25                 webClient = new WebClient();
      26                 //初始化读取数据流
      27                 myFileStream = new FileStream(localFilePath, FileMode.Open, FileAccess.Read);
      28                 myBinaryReader = new BinaryReader(myFileStream);
      29                 byte[] postArray = myBinaryReader.ReadBytes((int)myFileStream.Length);
      30                 //打开远程Web地址,将文件流写入
      31                 writeStream = webClient.OpenWrite(targetUrl, "PUT");
      32                 writeStream.Write(postArray, 0, postArray.Length);
      33             }
      34             finally
      35             {
      36                 if (writeStream != null) { writeStream.Close(); }
      37                 if (myBinaryReader != null) { myBinaryReader.Close(); }
      38                 if (myFileStream != null) { myFileStream.Close(); }
      39                 if (webClient != null) { webClient.Dispose(); }
      40             }
      41         }
      42 
      43         /// <summary>  
      44         /// 下载服务器文件至客户端
      45         /// </summary>  
      46         /// <param name="strUrlFilePath">要下载的Web服务器上的文件地址</param>  
      47         /// <param name="strLocalDirPath">存放下载文件的本地目录</param>
      48         /// <returns>True/False是否上传成功</returns>  
      49         public static void DownLoadFile(string strUrlFilePath, string strLocalDirPath)
      50         {
      51             //get file name
      52             string fileName = Path.GetFileName(strUrlFilePath);
      53             if (string.IsNullOrEmpty(fileName))
      54             {
      55                 throw new InvalidOperationException("the file name is empty");
      56             }
      57 
      58             //判断本地目录是否存在,如不存在,则创建新目录
      59             if (!Directory.Exists(strLocalDirPath))
      60             {
      61                 Directory.CreateDirectory(strLocalDirPath);
      62             }
      63 
      64             string localFilePath = Path.Combine(strLocalDirPath, fileName);
      65 
      66             using (var client = new WebClient())
      67             {
      68                 client.DownloadFile(strUrlFilePath, localFilePath);
      69             }
      70         }
      View Code
    2. 从FTP服务器下载资源
       1 public static class FTPHelper
       2 {
       3         /// <summary>
       4         /// Uri serverUri = new Uri("ftp://127.0.0.1/LocalUser/Paul1/V0.0.0.0.zip");
       5         /// </summary>
       6         /// <param name="uri"></param>
       7         public static void DownLoadFile(Uri serverUri,string downLoadFolderPath,string username,string password) 
       8         {
       9             //temp file path
      10             string tempFilePath = Path.Combine(downLoadFolderPath, Path.GetFileName(serverUri.LocalPath));
      11 
      12             // The serverUri parameter should start with the ftp:// scheme.
      13             if (serverUri.Scheme != Uri.UriSchemeFtp)
      14             {
      15                 throw new ArgumentException("uri must be ftp scheme");
      16             }
      17             // Get the object used to communicate with the server.
      18             WebClient request = new WebClient();
      19 
      20             request.Credentials = new NetworkCredential(username,password);
      21 
      22             //read stream
      23             Stream stream = null;
      24             //write stream
      25             Stream fileStream = null;
      26             try
      27             {
      28                 //if local file created.delete and create a new file.
      29                 if (File.Exists(tempFilePath))
      30                 {
      31                     File.Delete(tempFilePath);
      32                 }
      33                 fileStream = File.Create(tempFilePath);
      34 
      35                 //read data from server
      36                 stream = request.OpenRead(serverUri);
      37                 int readLength = 1024;
      38                 byte[] bytes = new byte[readLength];
      39                 while (stream.Read(bytes, 0, readLength) > 0)
      40                 {
      41                     //write data to file
      42                     fileStream.Write(bytes, 0, bytes.Length);
      43                 }
      44             }
      45             catch (Exception e)
      46             {
      47                 throw e;
      48             }
      49             finally
      50             {
      51                 if (fileStream != null) { fileStream.Close(); }
      52                 if (stream != null) { stream.Close(); }
      53             }
      54         }
      55     }
  • 相关阅读:
    django项目一:基于django2.2可重用登录与注册模块-图片验证码
    django项目一:基于django2.2可重用登录与注册模块-Django表单
    django项目一:基于django2.2可重用登录与注册模块-登录视图函数
    谈谈我的移动端rem适配方案
    微信公众号jssdk自定义分享,二次分享自定义失败解决技巧
    html-webpack-plugin的使用
    formData 无需form异步上传多个图片
    原生javascript跨浏览器常用事件处理
    apache+php+mysql运行环境
    MySQL企业级备份
  • 原文地址:https://www.cnblogs.com/JustYong/p/4086751.html
Copyright © 2011-2022 走看看