File download from SFTP server on ASP.NET page

2019-05-31 01:59发布

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条回答
forever°为你锁心
2楼-- · 2019-05-31 02:26

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:

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);
查看更多
登录 后发表回答