我能够从 sftp 获取文件流,但无法从控制器的浏览器(chrome)ui 中呈现该文件流。
如果我将文件存储在磁盘中然后返回它的工作正常但我想通过直接返回流而不是存储任何文件来做到这一点
下面是我的代码
公共 ActionResult 视图(字符串路径){
string contentType = "";
var filePath = Server.MapPath(path);
switch (System.IO.Path.GetExtension(filePath).ToLower())
{
case ".htm":
case ".html":
contentType = "text/HTML";
break;
case ".txt":
contentType = "text/plain";
break;
case ".doc":
case ".rtf":
case ".docx":
contentType = "Application/msword";
break;
case ".xls":
case ".xlsx":
contentType = "Application/x-msexcel";
break;
case ".jpg":
case ".jpeg":
contentType = "image/jpeg";
break;
case ".gif":
contentType = "image/GIF";
break;
case ".png":
contentType = "image/png";
break;
//case ".bmp":
// contentType = "image/x-ms-bmp";
// break;
case ".pdf":
contentType = "application/pdf";
break;
case ".bmp":
contentType = "image/bmp";
break;
}
try
{
Stream file = null;
SftpClient conn = null;
if (contentType != "")
{
conn = GetSFTPConnection();
conn.Connect();
file = new MemoryStream();
if (conn.Exists(path))
{
conn.DownloadFile(path, file);
}
else
{
throw new SftpPathNotFoundException();
}
return File(file, contentType);
}
}
catch (Exception)
{
throw new Exception("Unable To Get Requested File");
}
return null;
}