P
P
Peter2020-02-03 00:04:07
C++ / C#
Peter, 2020-02-03 00:04:07

How to get a list of files via WebApi in C#?

Goodnight. Can you please tell me how can I get a list of files via webapi?
I have a method in the controller that should just return me a list of files in a specific folder, but the path to the files should be like
localhost:5000/api/files/file1.txt
localhost:5000/api/files/file2.txt
and so on so that the client can download them.
I use NetCore 3.1, there used to be a Server.MapPath method, now it has been removed.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2020-02-03
@Morpheus_God

List of files in a directory - https://docs.microsoft.com/en-us/dotnet/api/system...
send a file

[HttpGet("{id}")]
public async Task<FileStreamResult> Download(int id)
{
    var filename = "file"+id.ToString()+".txt";
    var path = "Путь к файлу";
    var stream = File.OpenRead(path);
    return new FileStreamResult(stream, "application/octet-stream")
   {
       FileDownloadName = fileName
   };
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question