Downloading file via FTP fails (only downloads portion of file)

adarom1987

Below is a method to download content from an FTP, but if I just let the program run, it fails. If I step slowly through the code, it works. If I just let it run on its own, it only downloads 5kb of the file, and then moves on. It doesn't throw an exception, it just downloads the 5kb and then quits, moving onto the next item.

private static void DownloadFtpFile(string sourceFileLocation)
    {
        try
        {
                int bufferSize = 1024 * 300;
                int totalBytes = 0;
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(sourceFileLocation);
                long contentLength = webRequest.GetResponse().ContentLength;
                Console.WriteLine(totalBytes);

                using (WebResponse webResponse = webRequest.GetResponse())
                using (Stream reader = webResponse.GetResponseStream())
                using (BinaryWriter fileWriter = new BinaryWriter(File.Create(Application.StartupPath + "\\" + "tempFldr" + "\\" + "tempFile")))
                {
                    int bytesRead = 0;
                    byte[] buffer = new byte[bufferSize];
                    do
                    {
                        bytesRead = reader.Read(buffer, 0, buffer.Length);
                        totalBytes += bytesRead;
                        fileWriter.Write(buffer, 0, bytesRead);
                        Console.WriteLine("BytesRead: " + bytesRead + " -- TotalBytes: " + totalBytes);

                    } while (bytesRead > 0);
                }
            }
        catch (WebException ex)
        {
            String status = ((HttpWebResponse)ex.Response).StatusDescription;
            Console.WriteLine(status);
        }
    }
John Boling

To start, try switching from HttpWebRequest to FtpWebRequest. Not sure if that will make a huge difference, but if you're getting stuff from an FTP it may.

I feel like you might change from using a do-while to a while with something like this:

byte[] buffer = new byte[bufferSize];
int bytesRead;
while ((bytesRead = reader.Read(buffer, 0, buffer.Length)) > 0)
{
    fileWrite.Write(chunk, 0, bytesRead);
    totalBytes += bytesRead;
    Console.WriteLine("BytesRead: " + bytesRead + " -- TotalBytes: " + totalBytes);
}

Something else you may consider, maybe just download the file to a stream, then save it to the file after you've already closed the FTP connection.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Patch only a portion of a file

From Dev

downloading file using ftp

From Dev

Downloading a file via cURL fails , while using chrome it works

From Dev

How to decompress only a portion of a file?

From Dev

Java FTP client not downloading file

From Dev

Downloading file to Downloads folder isn't saving

From Dev

Are all internet file downloads using FTP protocol?

From Dev

Read only file-system error while downloading file from FTP in Android

From Dev

FTP via batch file

From Dev

file is not downloading via DownloadManager in Android

From Dev

file is not downloading via DownloadManager in Android

From Dev

Printing only a portion of the column contents .txt file

From Dev

Take only a portion of rows in an Excel file

From Dev

Getting day of year and downloading files via ftp using DOY as part of file name

From Dev

Downloading file from FTP using cURL

From Dev

Downloading txt file from ftp server

From Dev

PowerShell WinSCP FTP script is downloading cached file for only 1 out of 8 files

From Dev

Creating a multi-line file and downloading with Chrome.downloads.download

From Dev

Downloading simple text/csv file in Chrome fails

From Dev

(550) File unavailable while downloading file using FTP

From Dev

upload file via ftp from ftp in PHP

From Dev

Python Popen downloads only one file

From Dev

How to overwrite file via FTP

From Dev

How to overwrite file via FTP

From Dev

Downloading Most Recent File On Server Via wget

From Dev

Downloading a file from a PHP server via a website

From Dev

creating excel file via php and downloading it in android

From Dev

CasperJS and downloading a file via iFrame and JavaScript

From Dev

Size of file differs when downloading via curl

Related Related

HotTag

Archive