【发布时间】:2018-01-27 23:45:32
【问题描述】:
在我的 .Net Framework 4.5 项目之一中,我一直在创建响应 (file download),如下所示
.Net Framework 4.5 版本
public virtual HttpResponseMessage ExportExcel()
{
...................
....................
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);
Stream stream = new MemoryStream(bytes);
response.Content = new StreamContent(stream);
var cookie = new CookieHeaderValue("customValue", "true");
cookie.Path = "/";
response.Headers.AddCookies(new CookieHeaderValue[] { cookie });
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = ExportFileName
};
return response;.
}
如您所见,我正在添加 cookie、Header ContentType 等。
这是我移植的 .Net Core 版本。
public virtual ActionResult ExportExcel()
{
........
.......
byte[] bytes = _svc.Export(excelTemplatePath, ExcelColumns);
Response.StatusCode = 200;
Response.Cookies.Append("customValue", "true");
Response.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet").ToString();
return File(bytes, "application/x-msdownload", "");
}
}
问题:
1- 我找不到设置
ContentDisposition的方法2- 没有
Response.Content(我找不到 Response.Content = .Net Core 中的新 StreamContent(stream)。
【问题讨论】:
标签: c# .net asp.net-core