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
?