Ftp.DownloadFile .xlsx文件一旦下载就会损坏文件

问题描述 投票:0回答:1

使用以下代码将文件下载到服务器时出现问题。我尝试同时使用.txt文件和.xlsx文件。

仅针对.xlsx文件会出现此问题,而无法正常下载的.txt文件会出现。

(要提到。我已经使用FileZilla客户端从服务器手动下载了文件,只是为了查看文件没有损坏,但是一旦下载就可以了。我可以在excel中打开它)

[当我尝试使用我的代码打开下载的.xlsx文件时,该文件已损坏,excel表示:此文件已损坏,无法打开

我不知道为什么会这样。 excel文件包含1个图像和文本,我已经尝试了两个具有相同结果:request.UseBinary = true; request.UseBinary = false;

代码是:

            //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;
        }
c# ftpwebrequest downloadfileasync
1个回答
0
投票

我试图将代码的下部更改为下面的内容,现在可以使用。

                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;
                            }
                        }
                    }
                }
© www.soinside.com 2019 - 2024. All rights reserved.