Andreas
Andreas

Reputation: 1279

Ftp.DownloadFile .xlsx file corrupts the file once downloaded

I have a problem when downloading file to server using the below code.
I have tried with both a .txt file and .xlsx file.

The problem only occurs for the .xlsx file, Not the .txt file which works fine to download.

(To mention. I have downloaded the file from the server manually using the FileZilla client just to see that the file is not corrupted but this works fine once downloaded. I can open it in excel)

When I try to open the downloaded .xlsx file using my code, the file is corrupted and excel says:

This file is corrupt and cannot be opened

I wonder why this is happening. The excel file contains 1 image and text and I have tried both those with the same result:
request.UseBinary = true; request.UseBinary = false;

Code is:

            //Dowwload file
            Task<bool> task = FtpDownloadFile("ftp://someurl.com", "ftp://someurl.com/Folder1/r-invoice.xlsx", "C:/Folder1/ToDo Files/r-invoice.xlsx", "user_name", "password");
            if (task.IsFaulted == false)
            {

            }


        public async Task<bool> FtpDownloadFile(String host, String webbaddress, String destinationFile, String user_name, String password)
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
            bool returnvalue = false;
            try
            {
                var ext = Path.GetExtension(webbaddress);
                var imageExtensions = new[] { ".jpg", ".jpeg", ".png", ".gif", ".bmp", ".xlsx" };
                var isimage = imageExtensions.Contains(ext);

                var request = (FtpWebRequest)WebRequest.Create(webbaddress);
                request.Credentials = new NetworkCredential(user_name, password);
                request.UseBinary = isimage;
                request.Method = WebRequestMethods.Ftp.DownloadFile;
                request.ConnectionGroupName = host.Replace(".", "").Replace(":", "").Replace("/", "").Replace("-", "").Replace("_", "") + user_name;
                request.ServicePoint.ConnectionLimit = 4;

                using (var responseWeb = await request.GetResponseAsync())
                {
                    var response = (FtpWebResponse)responseWeb;
                    if (response.StatusDescription.Contains("150")) //150 Opening ASCII mode data connection for
                    {
                        using (var responseStream = response.GetResponseStream())
                        {
                            using (StreamReader reader = new StreamReader(responseStream))
                            using (StreamWriter destination = new StreamWriter(destinationFile))
                            {
                                destination.Write(reader.ReadToEnd());
                                destination.Flush();
                                returnvalue = true;
                            }
                        }
                    }
                }
            }
            catch (WebException ex) { String status = ((FtpWebResponse)ex.Response).StatusDescription; MessageBox.Show(status.ToString()); }
            return returnvalue;
        }

Upvotes: 0

Views: 597

Answers (1)

Andreas
Andreas

Reputation: 1279

I tried to change the lower part of the code to the below and now it works.

                using (var responseWeb = await request.GetResponseAsync())
                {
                    var response = (FtpWebResponse)responseWeb;
                    if (response.StatusDescription.Contains("150")) //150 Opening ASCII mode data connection for
                    {
                        using (var responseStream = response.GetResponseStream())
                        {
                            using (Stream fileStream = File.Create(destinationFile))
                            {
                                responseStream.CopyTo(fileStream);
                                returnvalue = true;
                            }
                        }
                    }
                }

Upvotes: 0

Related Questions