FTP下载时遇到的问题

现在做的一个项目是用的silverlight做的。 但是遇到一个问题 就是FTP下载的时候  获取GetResponse时  是超时的响应。 在百度上搜了很多的资料都没有解决呢。 结果发现解决其实很简单。 见下测红色代码。


//FTP下载操作
        public bool getFile(string FTP, string Account, string Password,string FileName,string LocalPath) 
        {
            FtpWebRequest reqFTP;
            try
            {
                FileStream outputStream = new FileStream(LocalPath, FileMode.Create);
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + FTP + "/" + FileName));
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary = true;
                reqFTP.UsePassive = true;
                reqFTP.KeepAlive = true;

                reqFTP.Credentials = new NetworkCredential(Account, Password);
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();


                Stream ftpStream = response.GetResponseStream();
                long cl = response.ContentLength;
                int bufferSize = 2048;
                int readCount;
                byte[] buffer = new byte[bufferSize];


                readCount = ftpStream.Read(buffer, 0, bufferSize);
                while (readCount > 0)
                {
                    outputStream.Write(buffer, 0, readCount);
                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                }
                ftpStream.Close();
                outputStream.Close();
                response.Close();


                return true;
            }
            catch (Exception err)
            {
                ExceptionRecord("FTP下载", err.Message + "\r\n" + err.StackTrace);
                return false;
            }
        }


这个样子的FTP函数。 就能在c#中使用。 同时也能被silverlight的wcf层进行引用了~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值