Error download Zip File with size of 160MB via FTP in C#

215 Views Asked by At

I'm downloading a .zip for the library FTPLibrary using a my own synchronizer When performing debugging , I find the error in this code

Data: Visual Studio 2012. Windows Form Aplicattion - C#

try
    {
     //Give Message of Command
       NewMessageEventArgs e = new NewMessageEventArgs("COMMAND", "Download File", "RETR");
                        OnNewMessageReceived(this, e);
                        byte[] buffer = new byte[2048];
                        int read = 0;
                        Int64 TotalBytesRead = 0;
                        Int64 FileSize = this.GetFileSize(sourceFilename);
                        DownloadCanceled = false;
                        do
                        {
                            if (DownloadCanceled)
                            {
                                NewMessageEventArgs CancelMessage = new NewMessageEventArgs("RESPONSE", "Download Canceled.", "CANCEL");

                                DownloadCanceled = false;
                                OnNewMessageReceived(this, CancelMessage);
                                return false;
                            }

                            read = DownloadResponseStream.Read(buffer, 0, buffer.Length);
                            DownloadFileStream.Write(buffer, 0, read);
                            TotalBytesRead += read;
                            //Declare Event
                            DownloadProgressChangedArgs DownloadProgress = new DownloadProgressChangedArgs(TotalBytesRead, FileSize);

                            //Progress changed, Raise the event.
                            OnDownloadProgressChanged(this, DownloadProgress);

                            System.Windows.Forms.Application.DoEvents();

                        } while (!(read == 0));


                        //Get Message and Raise Event
                        NewMessageEventArgs NewMessageArgs = new NewMessageEventArgs("RESPONSE", DownloadResponse.StatusDescription, DownloadResponse.StatusCode.ToString());
                        OnNewMessageReceived(this, NewMessageArgs);

                        //Declare Event
                        DownloadCompletedArgs Args = new DownloadCompletedArgs("Successful", true);
                        //Raise Event
                        OnDownloadCompleted(this, Args);

                        DownloadResponseStream.Close();
                        DownloadFileStream.Flush();
                        DownloadFileStream.Close();
                        DownloadFileStream = null;
                        DownloadResponseStream = null;
                    }
                    catch (Exception ex)
                    {
                        //catch error and delete file only partially downloaded
                        DownloadFileStream.Close();
                        //delete target file as it's incomplete
                        targetFI.Delete();

                        //Decalre Event for Error
                        DownloadCompletedArgs DownloadCompleted = new DownloadCompletedArgs("Error: " + ex.Message, false);
                        //Raise Event
                        OnDownloadCompleted(this, DownloadCompleted);
                    }
                }

More Information, see the class FTPLibrary. Error on line 391.

El error está en :

read = DownloadResponseStream.Read(buffer, 0, buffer.Length);

The program is a synchronizer , at first download a .zip of 100 kb , and then download the 150 MB and we get the following error

Spanish: WebException: Se ha terminado la conexión: Error inesperado de recepción English: The underlying connection was closed: An unexpected error

This causes the program download the 150 MB and then jumps branded and error , causing that you can not use the file for future action and the program comes to an end . I think the problem is in the buffer but not how to fix it. When downloading the file, I connect to an FTP server with a Windows 2003 . Thank you and I hope you can help me .

0

There are 0 best solutions below