简体   繁体   English

如何使用c#从FTP下载大于1MB的文件

[英]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.

相关问题 如何使用C#从FTP下载大量文件? - How to Download Numerous Files from FTP using C#? 如何使用 c# winforms 从 ftp 文件夹下载所有文件? - How can I download all files from a ftp folder with c# winforms? [c#]我无法从ftp服务器下载文件,但我可以得到文件列表 - [c#]I cannot download the files from ftp server, but I can get list of files 将文件从 C# 中的 FTP 服务器下载到修改日期大于指定的本地文件夹 - Download files from FTP server in C# to local folder with modification date greater than specified 如何在 c# 中使用 Octokit.Net Git Data API 从/向主 GitHub 检索和更新大于 1MB 的文件 - How to retrieve and update a file > 1MB from/to master GitHub using Octokit.Net Git Data API within c# 如何从 FTP 获取文件(使用 C#)? - How can I get file from FTP (using C#)? 如何在c#中读取带有位图类的1MB .tif文件 - How to read 1MB .tif file with bitmap class in c# 使用 C# 通过 http 下载 ftp 文件? - Download ftp files over http using C#? 如何在C#中使用EWS下载> 1 MB电子邮件附件 - How to download >1 MB Email attachment using EWS in C# 在 C# 中将 PDF 压缩到更小的尺寸(即 1MB 到 300kb) - Compress PDF to Smaller Size(i.e 1MB to 300kb) in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM