asp.net-web-api – 如何从ASP.net 5 web api返回文件
发布时间:2020-12-16 00:09:32 所属栏目:asp.Net 来源:网络整理
导读:在asp.net 5中创建了wep api.我想回复Post请求的文件响应.但不是文件响应的样子 ` { "version": { "major": 1,"minor": 1,"build": -1,"revision": -1,"majorRevision": -1,"minorRevision": -1 },"content": { "headers": [ { "key": "Content-Disposition"
在asp.net 5中创建了wep api.我想回复Post请求的文件响应.但不是文件响应的样子
` { "version": { "major": 1,"minor": 1,"build": -1,"revision": -1,"majorRevision": -1,"minorRevision": -1 },"content": { "headers": [ { "key": "Content-Disposition","value": [ "attachment; filename=test.pdf" ] },{ "key": "Content-Type","value": [ "application/pdf" ] } ] },"statusCode": 200,"reasonPhrase": "OK","headers": [],"requestMessage": null,"isSuccessStatusCode": true }` 码: public HttpResponseMessage Post([FromBody]DocumentViewModel vm) { try { if (ModelState.IsValid) { var Document = _repository.GetDocumentByGuid(vm.DocumentGuid,User.Identity.Name); var Params = Helper.ClientInputToRealValues(vm.Parameters,Document.DataFields); var file = Helper.GeneratePdf(Helper.InsertValues(Params,Document.Content)); var result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(System.IO.File.ReadAllBytes(file)) }; result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") { FileName = "test.pdf" }; result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf"); return result; } } catch (Exception ex) { Response.StatusCode = (int)HttpStatusCode.BadRequest; return null; } Response.StatusCode = (int)HttpStatusCode.BadRequest; return null; } 如何将实际文件作为响应而不是JSON返回?我使用Postman作为测试客户端. 解决方法
使用IActionResult而不是HttpResponseMessage.并返回FileStreamResult,并使其工作.
遇到了一个新问题,该文件不是我用来自服务器的流打开的文件.但是会为此创造一个新问题. 继续:Return file from ASP.NET 5 Web API 谢谢 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- winform_使用ClickOnce生成安装程序包页面&程序发布新版
- asp.net – 如何自定义UseExternalSignInCookie?
- asp.net – 如何在Nuget中排除/不显示.NET Framework包?
- asp.net – 使用成员资格数据在aspnet_profile表中存储其他
- asp.net-mvc-3 – MVC3 / Razor缩略图/调整大小图像想法?
- 在asp.net中设置MIME类型
- asp.net – 编译器失败,错误代码为-1073741819
- asp.net – 实体框架与存储过程
- asp.net-mvc – MVC @ Html.Display()
- asp.net-mvc – 用于表单提交和即时结果显示的良好ASP.NET
推荐文章
站长推荐
- asp.net-core – project.json中两个依赖项部分之
- asp.net – 表单身份验证保护什么,而不是使用会话
- asp.net-core – 为什么.net core 2.1 SPA有3个不
- asp.net-mvc-4 – 通过ADAL JavaScript Ajax和Kn
- 如何创建ASP.NET Web场?
- asp.net – 使用窗口域帐户进行身份验证的Web应用
- asp.net-mvc-3 – MVC2到MVC3 IOC问题
- asp.net – 如何为同一个按钮运行客户端和服务器
- asp.net-mvc – redirectToAction()和View()之间
- asp.net – System.InvalidOperationException:
热点阅读