小编Ate*_*efB的帖子

如何从web api控制器返回文件?

我正在使用MVC 5 web Api Controller,我想返回一个文件:

[Route("")]
public HttpResponseMessage GetFile()
{
    var statusCode = HttpStatusCode.OK;
    FileStream file = XLGeneration.XLGeneration.getXLFileExigence();

    return Request.CreateResponse(statusCode, file);
}
Run Code Online (Sandbox Code Playgroud)

它不起作用.

邮递员的例外是:

"ExceptionMessage":"'ObjectContent`1'类型无法序列化内容类型'application/json; charset = utf-8'的响应主体."

c# asp.net-mvc json asp.net-web-api

6
推荐指数
2
解决办法
2万
查看次数

如何在ASP MVC中下载由NPOI生成的xls文件

我找到了如何将Excel电子表格流回客户端但在aspx代码中的例子.代码如下

using (var exportData = new MemoryStream())
{
workbook.Write(exportData);
string saveAsFileName = string.Format("MembershipExport-{0:d}.xls",  DateTime.Now).Replace("/", "-");
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", saveAsFileName));
Response.Clear();
Response.BinaryWrite(exportData.GetBuffer());
Response.End();
}
Run Code Online (Sandbox Code Playgroud)

我正在使用asp MVC 5和webApi Controller.我想将此代码迁移到返回HttpResponseMessage的WebApiController.有什么好主意吗?

c# asp.net-mvc npoi asp.net-web-api

3
推荐指数
1
解决办法
4070
查看次数

标签 统计

asp.net-mvc ×2

asp.net-web-api ×2

c# ×2

json ×1

npoi ×1