老外那弄来的一段FTP上传的代码

 最近做了个仿网易的图片上传控件,为ftp上传伤了不少脑筋。在网上找了很多这方面的资料,但都不是很理想。后来在codeproject上找到了个老外写的图片上传控件,在上面剽窃了段代码过来用,没有国内网友发的那么多累赘代码,效果也很好!

  1. private void Upload(string filename)
  2.         {
  3.             FileInfo fileInf = new FileInfo(filename);
  4.             string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
  5.             FtpWebRequest reqFTP;
  6.             // Create FtpWebRequest object from the Uri provided
  7.             reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/wwwroot/upload/test/" + fileInf.Name));
  8.             // Provide the WebPermission Credintials
  9.             reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  10.             // By default KeepAlive is true, where the control connection is not closed
  11.             // after a command is executed.
  12.             reqFTP.KeepAlive = false;
  13.             // Specify the command to be executed.
  14.             reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
  15.             // Specify the data transfer type.
  16.             reqFTP.UseBinary = true;
  17.             // Notify the server about the size of the uploaded file
  18.             reqFTP.ContentLength = fileInf.Length;
  19.             // The buffer size is set to 2kb
  20.             int buffLength = 2048;
  21.             byte[] buff = new byte[buffLength];
  22.             int contentLen;
  23.             // Opens a file stream (System.IO.FileStream) to read the file to be uploaded
  24.             FileStream fs = fileInf.OpenRead();
  25.             try
  26.             {
  27.                 // Stream to which the file to be upload is written
  28.                 Stream strm = reqFTP.GetRequestStream();
  29.                 // Read from the file stream 2kb at a time
  30.                 contentLen = fs.Read(buff, 0, buffLength);
  31.                 // Till Stream content ends
  32.                 while (contentLen != 0)
  33.                 {
  34.                     // Write Content from the file stream to the FTP Upload Stream
  35.                     strm.Write(buff, 0, contentLen);
  36.                     contentLen = fs.Read(buff, 0, buffLength);
  37.                 }
  38.                 // Close the file stream and the Request Stream
  39.                 strm.Close();
  40.                 fs.Close();
  41.             }
  42.             catch (Exception ex)
  43.             {
  44.                 MessageBox.Show(ex.Message, "Upload Error");
  45.             }
  46.         }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【1】项目代码完整且功能都验证ok,确保稳定可靠运行后才上。欢迎下载使用!在使用过程中,如有问题或建议,请及时私信沟通,帮助解答。 【2】项目主要针对各个计算机相关专业,包括计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网等领域的在校学生、专业教师或企业员工使用。 【3】项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 【4】如果基础还行,或热爱钻研,可基于此项目进行二次开发,DIY其他不同功能,欢迎交流学习。 【注意】 项目下载解压后,项目名字和项目路径不要用中文,否则可能会出现解析不了的错误,建议解压重命名为英文名字后再运行!有问题私信沟通,祝顺利! 基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值