Search code examples
c#asp.net-corerestsharp

Streaming large file across multiple layers


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


Solution

  • 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
    };