Reputation: 1675
I have a web api which retrieves PDF file stored in database.
[HttpGet("file/{id}")]
public IActionResult GetFile(int id)
{
var file = dataAccess.GetFileFromDB(id);
HttpContext.Response.ContentType = "application/pdf";
FileContentResult result = new FileContentResult(file, "application/pdf")
{
FileDownloadName = "test.pdf"
};
return result;
}
I need to write a wrapper web api which will pass result from the above web api to the client.
What is the best way to achieve this using .NET Core? should I return byte array from the above web api and convert byte array to FileContentResult in the wrapper api?
Any code sample would be very helpful.
Thanks.
Upvotes: 0
Views: 2657
Reputation: 18265
You may try to just reroute the response from an HttpClient
to the user:
using System.Net.Http;
using Microsoft.AspNetCore.Mvc;
public class TestController : Controller
{
public async Task<IActionResult> Get()
{
using (var client = new HttpClient())
{
var response = await client.GetAsync("http://apiaddress", HttpCompletionOption.ResponseHeadersRead); // this ensures the response body is not buffered
using (var stream = await response.Content.ReadAsStreamAsync())
{
return File(stream, "application/pdf");
}
}
}
}
Upvotes: 1