[英]How can I download files greater that 1MB from FTP using c#
I have created a C# application for downloading file from FTP. 我创建了一个C#应用程序,用于从FTP下载文件。 I could not download files having size greater than 1 MB.
我无法下载大小大于1 MB的文件。 I got an error "Data is corrupted" while downloading the file.
下载文件时出现错误“数据已损坏”。 Here is my code for download.
这是我的下载代码。
int count = 0;
do
{
byte[] buffer = new byte[1024];
conn.ReadTimeout = 15000000;
count = istream.Read(buffer, 0, buffer.Length);
memoryStream.Write(buffer, 0, count);
} while (istream.CanRead && count > 0);
byteArray = memoryStream.ToArray();
var workBook = new XLWorkbook(memoryStream);
var workSheet = workBook.Worksheets.First();
The problem with your code that when you read data from istream and in there is no more data it will return count 0, and you need to exit your loop, instead of reading data to the memoryStream. 代码的问题在于,当您从istream读取数据且其中没有更多数据时,它将返回计数0,并且您需要退出循环,而不是将数据读取到memoryStream。
Try this: 尝试这个:
while(true)
{
byte[] buffer = new byte[1024];
conn.ReadTimeout = 15000000;
count = istream.Read(buffer, 0, buffer.Length);
if (count == 0)
break;
memoryStream.Write(buffer, 0, count);
}
istream.Close();
byteArray = memoryStream.ToArray();
var workBook = new XLWorkbook(memoryStream);
var workSheet = workBook.Worksheets.First()
Also close your istream after the loop, otherwise downloaded file might get truncated and that might be the reason why you are getting "Data is corrupted" error. 循环后也请关闭istream,否则下载的文件可能会被截断,这可能就是您收到“数据已损坏”错误的原因。
Its an issue with firewall blocking from my system on uploading data to FTP server. 将数据上传到FTP服务器时,防火墙阻止了我的系统,这是一个问题。 The uploaded data is corrupted on the FTP server and I am trying to download the same corrupted data using my code.
FTP服务器上的上传数据已损坏,我正在尝试使用我的代码下载相同的损坏数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.