Reputation: 89
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?
Upvotes: 2
Views: 1946
Reputation: 89
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.
Upvotes: 2
Reputation: 956
Your progress bar is triggering the failure, trying to set it to -45 which is invalid.
int totalSize = (int)(fileSize / 1048576);
bgftpdownload.ReportProgress((bytes / 1048576) * 100 / totalSize, totalSize);
You're getting an overflow on totalSize, which is causing the negative % which is causing the error you're seeing.
... at least I think that's what's wrong.
Upvotes: 0