File download from SFTP server on ASP.NET page

一笑奈何 提交于 2020-01-02 20:08:42

问题


I have SFTP server on Ubuntu 16.04 and a site with REST API on ASP Core 2.0.

The problem is that I want to provide a straight link to a file on the server, but I don't know how to do it. I suppose it has to be done by API, but what the way? I can download the file by API and give it to the user, but there will be twice time (for download by API from the server and by user from API). And I saw on some sites a link to the file with some access token. May be I should use this way? Any ideas?


回答1:


but there will be twice time

Not necessarily.

You just have to use a streaming API and pass the data continuously from an input SFTP stream to an output HTTP stream.

Here are some examples for FTP:

  • Download file from FTP and how prompt user to save/open file in ASP.NET C#
  • Streaming Zip on ASP.NET Core

With SFTP it will be the same, just that you need to use an SFTP library (there's no SFTP support in .NET) instead of FtpWebRequest.

Particularly SSH.NET library has a streaming API. Check its SftpClient.DownloadFile method:

public void DownloadFile(string path, Stream output, Action<ulong> downloadCallback = null)

You can probably use it simply like:

sftpClient.DownloadFile(path, Response.OutputStream);


来源:https://stackoverflow.com/questions/50851959/file-download-from-sftp-server-on-asp-net-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!