0

I have a method in some micro service in C#:

[HttpGet]
[ProducesResponseType(typeof(FileStreamResult), StatusCodes.Status200OK, "application/pdf")]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> Get(long id, CancellationToken cancellationToken)
{
    var result = await service.RenderIfExists(id, cancellationToken);

    if (result is null)
    {
        return NotFound();
    }
 
    Response.Headers.Append("Access-Control-Expose-Headers", "Content-Disposition");

    return File(fileContents: result,contentType: "application/pdf",fileDownloadName: $"lalala");
}

When I invoke this endpoint from Swagger, I can download the returned file.

I have to just invoke this endpoint from another micro service with GetStreamAsync and return the result as is. But I don't understand how. I also have to do this using stream to avoid saving result in LOH.

public async Task<IActionResult> Get(long id, CancellationToken token)
{
    const string @base = "lalala";
    var url = $"lalala";
    var path = @base + url;
    var stream = await httpClient.GetStreamAsync(path, token);
    // ???
}
5
  • 2
    Results.Stream(Stream, ...) ? Commented May 23, 2024 at 14:31
  • @MarcGravell, thank you a lot!! That works almost like I need. Now I recieve result with "Response headers: content-type: application/octet-stream". How can I get it as when I invoke first endpoint: Response headers content-disposition: attachment; filename="___ _______ _ ________ _ 1"; filename*=UTF-8''%D0%90%D0%BA%D1%82%20%D0%BE%D1%81%D0%BC%D0%BE%D1%82%D1%80%D0%B0%20%D0%B8%20%D0%B2%D1%81%D0%BA%D1%80%D1%8B%D1%82%D0%B8%D1%8F%20%E2%84%96%201 content-length: 72593 content-type: application/pdf Commented May 23, 2024 at 14:43
  • In other words, I lose contentType and fileDownloadName Commented May 23, 2024 at 14:46
  • I've tried "return Results.Stream(stream, contentType: "application/pdf", fileDownloadName: "lalala");", but I received "Unrecognized response type; displaying content as text." :( Commented May 23, 2024 at 14:53
  • I've solved the problem! I've wrote "Response.Headers.Append("Access-Control-Expose-Headers", "Content-Disposition");" Commented May 23, 2024 at 15:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.