0

I am trying to redirect from my MVC application to a FileStreamResult in my web api. The MVC side needs to do a post because of the data that is being sent over. The basic idea is that I should be able to post in the MVC side and what happens next is the API side will create the file and allow me to download/view from there. With what I have it makes the request to the API side but stays on the MVC side. My approximate code is below, any thoughts?

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");
    }
DrivenTooFar
  • 110
  • 1
  • 1
  • 11
  • In the code above you are returning "OK" to the browser from the MVC action. The status code you're setting on the response from your API call is irrelevant. – Craig H Feb 14 '20 at 16:09
  • Thanks. I realized what was in my MVC part wasn't really working and I updated it. Still get the same result but it hits the API. – DrivenTooFar Feb 14 '20 at 16:45

0 Answers0