Download file prompt when using WebAPI HttpResponseMessage

后端 未结 1 1810
攒了一身酷
攒了一身酷 2021-01-07 05:07

I have a method in my API that returns a HttpResponseMessage:

    [HttpGet, HoodPeekAuthFilter]
    public HttpResponseMessage GlobalOverview()
    {
                


        
相关标签:
1条回答
  • 2021-01-07 05:14

    In your MVC controller you can try returning a FileResult, and use the File() method reading the response of your API as byte array.

    [Authorize]
    [HttpGet]
    public FileResult GlobalOverview()
    {
        HttpResponseMessage file = new HttpResponseMessage();
        using (HttpClient httpClient = new HttpClient())
        {
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(UTF8Encoding.UTF8.GetBytes(this.UserName + ':' + this.Password)));
            Task<HttpResponseMessage> response = httpClient.GetAsync("api.someDomain/Reporting/GlobalOverview");
            file = response.Result;
        }
    
        return File(file.Content.ReadAsByteArrayAsync().Result, "application/octet-stream", "GlobalOverview.csv");
    }
    
    0 讨论(0)
提交回复
热议问题