-2

Large zip file (in gigabytes) is stored in API layer. When a user clicks download button in the browser the request goes through WEB tier to the API tier and in return we need to stream the large file from API tier to WEB tier back to the client browser.

Please advice how can I stream large file from API application to WEB application to client without writing the file in web application?

The Web application request API applications using rest sharp library, it would be great if you can advice a solution using rest sharp (alternatively native code). Both the projects are in .NET core 2.2

Jemin
  • 533
  • 4
  • 12
  • 25

2 Answers2

1

Are you looking for DownloadData?

https://github.com/restsharp/RestSharp/blob/master/src/RestSharp/RestClient.Sync.cs#L23

The following is directly from the example in the docs:

var tempFile = Path.GetTempFileName();
using var writer = File.OpenWrite(tempFile);

var client = new RestClient(baseUrl);
var request = new RestRequest("Assets/LargeFile.7z");
request.ResponseWriter = responseStream =>
{
    using (responseStream)
    {
        responseStream.CopyTo(writer);
    }
};
var response = client.DownloadData(request);
Wes Doyle
  • 2,199
  • 3
  • 17
  • 32
  • This would work if it is one layer that is streaming a file and downloading, but I need to read a file from app tier and stream it to web tier and from web tier i need to stream again to client – Jemin Jan 13 '20 at 17:00
  • 1
    @Jemin In that case just make a new `MemoryStream` rather than a `FileStream`, e.g. replace `using var writer = File.OpenWrite(tempFile);` with something like `var writer = new MemoryStream();`. That'll populate the `MemoryStream` when `DownloadData` is called and you can return the steam to the caller. – Matthew Sep 26 '21 at 16:24
0

Found the solution by using HttpClient instead of RestSharp library for downloading the content directly to browser

The code snippet is as below

HttpClient client = new HttpClient();
var fileName = Path.GetFileName(filePath);
var fileDowloadURL = $"API URL";

var stream = await client.GetStreamAsync(fileDowloadURL).ConfigureAwait(false);
// note that at this point stream only contains the header the body content is not read

return new FileStreamResult(stream, "application/octet-stream")
{
    FileDownloadName = fileName
};
Jemin
  • 533
  • 4
  • 12
  • 25
  • 1
    If you are going to stream from server to client and go back to server... at least is what you put in one of the comments... I do recommend to use MemoryStream instead of FileStream... it would perform better! – Gabriel Andrés Brancolini Jan 14 '20 at 16:57