从FTP C#下载大文件

编程入门 行业动态 更新时间:2024-10-25 04:22:19
本文介绍了从FTP C#下载大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用示例代码通过FTP下载文件,因为我的旧代码不适用于较大的文件,因为它立即捕获整个文件,这会导致内存不足错误. reader.read()要求提供(char [],int,int),就像我过去使用过的普通Read()方法一样,将其送入(byte [],int,int). 是否因为响应流而请求char []?以及如何创建与以前使用过的byte []类似的char []缓冲区? 我是否在正确的轨道上?

I am trying to download a file via FTP using sample code I found as my old code doesn''t work with larger files due to it grabbing the whole file at once which causes Out of Memory errors. The reader.read() is asking for (char[],int,int) i''m feeding it (byte[],int,int) like normal Read() methods I have used in the past. Is it asking for char[] because it''s a response stream? and How do I even create a char[] buffer that is simular to the byte[] I have used previously? Am I even on the right track?

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp01/temp.exe"); request.Method = WebRequestMethods.Ftp.DownloadFile; request.Credentials = new NetworkCredential("admin", "1234"); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader reader = new StreamReader(responseStream); FileStream file = File.Create(@"c:\temp\temp.exe"); byte[] buffer = new byte[32 * 1024]; int read; reader.Read( while ((read = reader.Read(buffer,0,buffer.Length)) > 0) { file.Write(buffer, 0, read); } file.Close(); reader.Close(); response.Close();

在此先感谢您的帮助.

Thanks in advance for any help.

推荐答案

我认为您离我们太远了.除了不完整的行,我看不到有很多行不通的地方.您遇到错误了吗? 此处 [ ^ ]一些与您正在做的事情非常接近的代码(降低一半). 改用此方法...不要与StreamReader一起使用,只需使用Stream. I don''t think you''re too far off. Other than the incomplete line, I don''t see much that won''t work there. Are you getting an error? Here''s[^] some code very close to what you''re doing (halfway down). Try this instead...don''t go with a StreamReader, just use the Stream. FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp01/temp.exe"); request.Method = WebRequestMethods.Ftp.DownloadFile; request.Credentials = new NetworkCredential("admin", "1234"); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); FileStream file = File.Create(@"c:\temp\temp.exe"); byte[] buffer = new byte[32 * 1024]; int read; //reader.Read( while ((read = responseStream.Read(buffer, 0, buffer.Length)) > 0) { file.Write(buffer, 0, read); } file.Close(); responseStream.Close(); response.Close();

干杯.

Cheers.

更多推荐

从FTP C#下载大文件

本文发布于:2023-11-07 22:36:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1567625.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:大文件   FTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!