Getting FileActionResult Between Services

I have a route that returns a PDF file to a FileActionResult file:

[HttpGet]
[Route("getPdf")]
public async Task<IHttpActionResult> GetPdf()
{
    return new FileActionResult(Resources.TestPdf, "test.pdf");
}

Here is my FileActionResult class:

public class FileActionResult : IHttpActionResult
{
    private byte[] File { get; set; }

    private string FileName { get; set; }

    public FileActionResult(byte[] file, string fileName)
    {
        File = file;
        FileName = fileName;
    }

    public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
    {
        var response = new HttpResponseMessage();
        response.Content = new StreamContent(new MemoryStream(File));
        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
        response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
        {
            FileName = FileName,
            DispositionType = "attachment"
        };

        return Task.FromResult(response);
    }
}

When I test my browser, the PDF file loads successfully. However, when I test another server using the following code, I get an error when accessing the returned stream:

public static Stream OpenReadStreamToRemoteFile(string url)
{    
    using (var client = new WebClient())
    {
        return client.OpenRead(url);
    }
}

What should I do differently in mine OpenReadStreamToRemoteFile?

+4
source share

Source: https://habr.com/ru/post/1682674/


All Articles