使用 SSH.NET 从 ASP.NET 中的 SFTP 服务器将文件下载到浏览器

Download file to browser from SFTP server in ASP.NET using SSH.NET

使用SSH.NET,我试图触发浏览器下载文件,而不是直接将其写入本地文件存储。

using(var saveFile = File.OpenWrite(@"Downloads")) {
  result = client.BeginDownloadFile(remotePath + fileName, saveFile) as SftpUploadAsyncResult;

  client.EndUploadFile(result);
}

以上代码失败,因为我的应用程序池身份没有写入文件的权限,而且我没有计算机的管理员权限,因此无法授予权限。

因此,更好的选择是通过浏览器触发下载,该浏览器之前支持 FtpWebRequest

但我目前的任务是将 FTP 下载转换为 SFTP 下载。

谁能推荐触发浏览器下载的最佳方式?

下载文件到HttpResponse.OutputStream:

HttpResponse response = HttpContext.Current.Response;

response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);

client.DownloadFile(remotePath + fileName, response.OutputStream);