当使用下载文件的提示的WebAPI的Htt presponseMessage [英] Download file prompt when using WebAPI HttpResponseMessage
问题描述
我在API的方法返回一个Htt的presponseMessage:
I have a method in my API that returns a HttpResponseMessage:
[HttpGet, HoodPeekAuthFilter]
public HttpResponseMessage GlobalOverview()
{
try
{
StatsRepo _statsRepo = new StatsRepo();
string file = _statsRepo.IncidentData().AsCSVString();
if (file == null)
{
return Request.CreateResponse(HttpStatusCode.NoContent);
}
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StringContent(file);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = "GlobalOverview.csv";
return result;
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError);
}
}
在我的MVC的Web Apllication我有一个需要调用API,并返回该文件的控制器动作:
In my MVC Web Apllication i have a controller Action that needs to call the API and return the file:
[Authorize]
[HttpGet]
public HttpResponseMessage 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;
}
如果我直接访问API网址它会提示我输入用户名和密码,然后保存文件出现的对话,我可以下载该文件。
If i access the API url directly it prompts me for a username and password, and then the Save File dialogue appears, and i can download the file.
如果我定位到行动在我的Web应用程序,然后我得到以下响应输出到屏幕上:
If i navigate to the Action in my Web Application then i get the following response output to screen:
StatusCode: 200,
ReasonPhrase: 'OK',
Version: 1.1,
Content: System.Net.Http.StreamContent,
Headers: { Pragma: no-cache X-SourceFiles: = XXXXXXX
Content-Disposition: attachment;
filename=GlobalOverview.csv
Content-Type: application/octet-stream Expires: -1
我把它我需要返回一个FileResult,但我不知道是如何转换的Htt presponseMessage到FileResult。
I take it i need to return a FileResult, but i have no clue as to how to convert the HttpResponseMessage to a FileResult.
推荐答案
在你的MVC控制器,你可以尝试返回FileResult,并使用文件()方法作为读取字节数组的API的响应。
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");
}
这篇关于当使用下载文件的提示的WebAPI的Htt presponseMessage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!