问题描述
我正在尝试使用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;
}
}