我正在使用必须返回excel文件的Web Api调用。为此,我有一个导出方法,它返回与来自正文的列表相对应的memorystream对象。我可以检查返回的流是否正常,但我无法打开下载文件的对话框。根据{{3}}帖子,我做得很好,唯一的区别是我没有文件的路径,但我想这没有必要。
[HttpPost()]
public HttpResponseMessage Export([FromBody]List<SomeClass> someList)
{
HttpResponseMessage response = null;
try
{
MemoryStream memoryStream = _someService.Export(someList);
response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StreamContent(memoryStream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "LineItemErrors.xlsx"
};
}
catch (Exception ex)
{
response = Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
}
return response;
}
响应消息随内容属性填充以及其他所有内容。我做错了什么?