3
我有一个基本的web api,试图返回一个作为下载动作触发的excel文档而不是数据流。关于创建流和设置附件属性,我在网上关注了很多示例,但我的邮差请求始终显示为已编码。以下是API方法。返回任务<IHttpActionResult>中的xlsx数据与EPPlus
[Route("getexcel")]
[HttpPost]
public async Task<IHttpActionResult> GetExcel()
{
IHttpActionResult result = null;
FileInfo newFile = new FileInfo(@"C:\Test.xlsx");
ExcelPackage pck = new ExcelPackage(newFile);
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new ByteArrayContent(pck.GetAsByteArray());
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.Content.Headers.ContentDisposition.FileName = "Test.xlsx";
result = ResponseMessage(response);
return result;
}
从上面的代码,如果我使用MediaTypeHeaderValue(“应用/ PDF”),则请求邮差示出下载请求但对于XLSX格式或应用程序/八位字节流没有。相反,它显示了一个数据流。 Content-Type是application/json,因为api的最终版本将在主体中使用json数据。
这是正确的解决方案,我意识到了这一点,几天后创建问题,忘记更新,谢谢回复。 –