MVC アプリケーションから Web API の FileStreamResult にリダイレクトしようとしています。データが送信されるため、MVC 側はポストを行う必要があります。基本的な考え方は、MVC 側に投稿できるようにする必要があり、次に何が起こるかというと、API 側がファイルを作成し、そこからダウンロード/表示できるようにすることです。私が持っているものでは、API 側にリクエストを行いますが、MVC 側にとどまります。私のおおよそのコードは以下のとおりです。
MVC
[HttpGet]
public HttpResponseMessage Download(int id)
{
var body = GetBodyForPost(id);
using (var client = new HttpClient())
{
var response = client.PostAsJsonAsync(url, body);
return response.Result.EnsureSuccessStatusCode();
}
}
API
[HttpPost]
[ActionName("Download")]
public async Task<FileStreamResult> Download([FromBody] IEnumerable<RequestData> data)
{
var stream = BuildFileFromData(data);
Response.Headers.Add("Content-Type", "application/pdf");
Response.Headers.Add("Content-Disposition", "attachment; filename=print.pdf");
return new FileStreamResult(stream, "application/pdf");
}