如何解决通过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扩展名文件,由于某种原因,当我尝试打开它时,收到了以下错误消息。
如果我使用的是IISExpress,则单击“是”按钮后仍然可以打开它,但是如果使用的是普通的IIS,则单击“是”按钮后它无法打开文件。
解决方法
我能够通过如下修改我的代码来解决此问题
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;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。