使用ftp协议下载大文件 [英] Download large files using ftp protocol

查看:590
本文介绍了使用ftp协议下载大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这可能不是第一次,这是问。
但是我找不到解决这个问题的方法..

  private void bgftpdownload_DoWork(object sender,DoWorkEventArgs e )
{
FtpWebRequest请求=(FtpWebRequest)WebRequest.Create(ftpurl +/+ clientlabel.Text +/data.7z);
request.Credentials = new NetworkCredential(ftpuser,ftppass);
request.Method = WebRequestMethods.Ftp.GetFileSize;
request.Proxy = null;

FtpWebResponse response =(FtpWebResponse)request.GetResponse();
long fileSize = response.ContentLength;

request =(FtpWebRequest)WebRequest.Create(ftpurl +/+ clientlabel.Text +/data.7z);
request.Credentials = new NetworkCredential(ftpuser,ftppass);
request.Method = WebRequestMethods.Ftp.DownloadFile;
using(FtpWebResponse responseFileDownload =(FtpWebResponse)request.GetResponse())
using(Stream responseStream = responseFileDownload.GetResponseStream())
using(FileStream writeStream = new FileStream(LocationFile,FileMode.Create ))
{

int长度= 2048;
Byte [] buffer = new Byte [Length];
int bytesRead = responseStream.Read(buffer,0,Length);
int bytes = 0;

while(bytesRead> 0)
{
writeStream.Write(buffer,0,bytesRead);
bytesRead = responseStream.Read(buffer,0,Length);
字节+ = bytesRead;
int totalSize =(int)(fileSize / 1048576);
bgftpdownload.ReportProgress((bytes / 1048576)* 100 / totalSize,totalSize);


$ b private void bgftpdownload_ProgressChanged(object sender,ProgressChangedEventArgs e)
{
progresslabel.Text = e.ProgressPercentage *(int)e .UserState / 100 +Mb /+ e.UserState +Mb;
progressBar1.Value = e.ProgressPercentage;
}

我有这个代码,它的工作很棒..直到它碰到一个2 gb文件在ftp服务器上

我可以在其他论坛上阅读,(int)的值限制为= 2147483591
所以问题关闭导致我的字节(2147483591)



System.Windows.Forms.dll中发生类型'System.ArgumentOutOfRangeException'的异常,但未在用户代码中处理

p>

其他信息:Værdien'-45'er ugyldig'Value'。 'Value'skalværemellem'minimum'og'maximum'。



我该如何解决这个问题?

解决方案

long bytes = 0;



bgftpdownload.ReportProgress((int)(bytes / 1048576)* 100 / totalSize,totalSize);

解决方案,我把(int)放在ReportProgress错误的地方。


i know this is probably not the first time, this is asked. But i can't find the solution to the problem..

  private void bgftpdownload_DoWork(object sender, DoWorkEventArgs e)
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpurl + "/" + clientlabel.Text + "/data.7z");
        request.Credentials = new NetworkCredential(ftpuser, ftppass);
        request.Method = WebRequestMethods.Ftp.GetFileSize;
        request.Proxy = null;

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        long fileSize = response.ContentLength;

        request = (FtpWebRequest)WebRequest.Create(ftpurl + "/" + clientlabel.Text + "/data.7z");
        request.Credentials = new NetworkCredential(ftpuser, ftppass);
        request.Method = WebRequestMethods.Ftp.DownloadFile;
        using (FtpWebResponse responseFileDownload = (FtpWebResponse)request.GetResponse())
        using (Stream responseStream = responseFileDownload.GetResponseStream())
        using (FileStream writeStream = new FileStream(LocationFile, FileMode.Create))
        {

            int Length = 2048;
            Byte[] buffer = new Byte[Length];
            int bytesRead = responseStream.Read(buffer, 0, Length);
            int bytes = 0;

            while (bytesRead > 0)
            {
                writeStream.Write(buffer, 0, bytesRead);
                bytesRead = responseStream.Read(buffer, 0, Length);
                bytes += bytesRead;
                int totalSize = (int)(fileSize / 1048576);
                bgftpdownload.ReportProgress((bytes / 1048576) * 100 / totalSize, totalSize);
            }
        }
    }
    private void bgftpdownload_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
            progresslabel.Text = e.ProgressPercentage * (int)e.UserState / 100 + " Mb / " + e.UserState + " Mb";
            progressBar1.Value = e.ProgressPercentage;
    }

I have this code, and its working great.. until its hitting a 2 gb file on the ftp server

I can read on other forums, the value limit for (int) is = 2147483591 So the problem is off cause my byte is getting higher than limit (2147483591)

An exception of type 'System.ArgumentOutOfRangeException' occurred in System.Windows.Forms.dll but was not handled in user code

Additional information: Værdien '-45' er ugyldig for 'Value'. 'Value' skal være mellem 'minimum' og 'maximum'.

What can i do to fix this problem?

解决方案

long bytes = 0;
and
bgftpdownload.ReportProgress((int)(bytes / 1048576) * 100 / totalSize, totalSize);
Was the solution, i placed the (int) in the ReportProgress the wrong place.

这篇关于使用ftp协议下载大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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