从FTP'System.OutOfMemoryException'C#下载900MB文件 [英] Download 900MB File from FTP 'System.OutOfMemoryException' C#

查看:119
本文介绍了从FTP'System.OutOfMemoryException'C#下载900MB文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过ftp下载900MB的文件.以下代码对于较小的文件似乎可以正常工作,但是在处理此较大的文件时出现了内存不足错误.任何帮助将不胜感激.

I am trying to download a 900MB file via ftp. The following code seems to work fine for smaller files but I am recieving an out of memory error when working with this large file. Any help would be appreciated.

WebClient request = new WebClient();
request.Credentials = new NetworkCredential("username", "password");

byte[] fileData = request.DownloadData("ftp://testftp01/downloadfile.exe");

FileStream file = File.Create(@"c:\temp\downloadfile.exe");                
file.Write(fileData, 0, fileData.Length);
file.Close();




在此先感谢您的帮助.




Thanks in advance for any help.

推荐答案

当您尝试创建一个包含943,718,400个元素(900 MB)的新数组时,您就会明白.数组中的元素过多.

WebClient (这是我假设您正在使用的东西)(因为您的代码清楚地表明这就是您正在使用的东西!)仅用于简单情况.

您将不得不切换到WebRequest并使用ResponseStream.然后,您可以逐字节读取它.

您也可以尝试WebClient的DownloadFile方法.由于它直接下载到文件,因此它可能能够处理较大的文件.
When you try to create a new array with 943,718,400 elements (900 MB), you get that. The array has too many elements.

WebClient (which is what I assume you''re using)(since your code clearly says that''s what you''re using!) is for simple situations.

You''ll have to switch to WebRequest and use a ResponseStream. Then, you can read it byte by byte.

You could also try the DownloadFile method of WebClient. Since it downloads directly to a file, it might be able to handle larger files.


默认情况下,请求大小约为1-2MB.为了下载/传送大小大于该大小的请求,需要进行配置设置.

尝试在配置文件中设置请求的大小.
在这里查看: http://msdn.microsoft.com/en-us/library/e1f13641.aspx [^ ]
http://msdn.microsoft.com/en-us/library/debx8sh9.aspx [ ^ ]

设置适当的MaxRequestLength属性并尝试.
By default request size is around 1-2MB. In order to download/transfer request having size more than that needs configuration setting.

Try setting size of request in config file.
Look here: http://msdn.microsoft.com/en-us/library/e1f13641.aspx[^]
http://msdn.microsoft.com/en-us/library/debx8sh9.aspx[^]

Set appropriate MaxRequestLength attribute and try.


这篇关于从FTP'System.OutOfMemoryException'C#下载900MB文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆