4

I have a route returning a pdf file as a FileActionResult:

[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 from my browser, the pdf file successfully downloads. However when I test from a different 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 be doing differently in my OpenReadStreamToRemoteFile?

jth41
  • 3,808
  • 9
  • 59
  • 109
  • 1
    You say you get "an error". Can you tell us what error? – Scotty H Jul 31 '17 at 15:31
  • I test your code and it works fine, and i can [creating a byte array from that stream](https://stackoverflow.com/questions/221925/creating-a-byte-array-from-a-stream#221941) , or [save the stream to the origin file](https://stackoverflow.com/questions/411592/how-do-i-save-a-stream-to-a-file-in-c#5515894). – NicoXiang Aug 01 '17 at 14:35

0 Answers0