通过SftpClient下载后,某些大的'.xlsx'扩展文件无法打开

问题描述

我正在尝试使用SftpClient将文件从远程linux服务器下载到本地计算机。

这是我下载文件代码

        public MemoryStream DownloadFile2(string path)
        {
            var connectionInfo = _taskService.GetBioinformaticsServerConnection();
            MemoryStream fileStream = new MemoryStream();
                        
            using (SftpClient client = new SftpClient(connectionInfo))
            {
                client.ConnectionInfo.Timeout = TimeSpan.FromSeconds(200);
                client.Connect();

                
                client.DownloadFile(path,fileStream);
                fileStream.Seek(0,SeekOrigin.Begin);
                
                var response = new MemoryStream(fileStream.GetBuffer());
                return fileStream;
            }
        }

这是调用上述方法的控制器。

        public FileResult DownloadFile(string fullPath,string fileName)
        {
            if (!string.IsNullOrEmpty(fileName))
            {
                fullPath = string.Concat(fullPath,"/",fileName);
            }
            var ms = _reportAPI.DownloadFile2(fullPath);

            var ext = Path.GetExtension(fullPath);
            if (ext == ".xlsx")
            {
                return File(ms,"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",fileName);
            }
            return File(ms,"application/octet-stream",fileName);            
        }

我已经设法对大多数文件进行了此操作,但是对于某些较大的.xlsx扩展名文件,由于某种原因,当我尝试打开它时,收到了以下错误消息。

enter image description here

如果我使用的是IISExpress,则单击“是”按钮后仍然可以打开它,但是如果使用的是普通的IIS,则单击“是”按钮后它无法打开文件

对于其他类型的文件或较小的excel文件,它可以正常工作。

有什么主意我该如何修改我的代码解决此问题?

解决方法

我能够通过如下修改我的代码来解决此问题

        public MemoryStream DownloadFile2(string path)
        {
            var connectionInfo = _taskService.GetBioinformaticsServerConnection();
            MemoryStream fileStream = new MemoryStream();
            byte[] fileBytes = null;
            using (SftpClient client = new SftpClient(connectionInfo))
            {
                client.ConnectionInfo.Timeout = TimeSpan.FromSeconds(200);
                client.Connect();

                client.DownloadFile(path,fileStream);

                fileBytes = fileStream.ToArray();
                
                var response = new MemoryStream(fileBytes);
                return response;
            }
        }