webapi返回文件流
逻辑说明
webapi返回类型为IHttpActionResult接口,内部方法返回HttpResponseMessage。
public interface IHttpActionResult
{
Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken);
}
参照JsonResult<T>,自定义返回文件流。
主要为:设置文件响应内容流,文件内容类型,文件名。
HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK);
httpResponseMessage.Content = new StreamContent(_stream);
httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue(_mediaType);
httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = HttpUtility.UrlEncode(_fileName, Encoding.UTF8),
};
完整代码
完整FileStreamResult如下:
public class FileStreamResult : IHttpActionResult
{
readonly Stream _stream;
readonly string _mediaType;
readonly string _fileName; public FileStreamResult(Stream stream, string mediaType) : this(stream, mediaType, null) { } public FileStreamResult(Stream stream, string mediaType, string fileName)
{
_stream = stream;
_mediaType = mediaType;
_fileName = fileName;
} public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
return Task.FromResult<HttpResponseMessage>(Execute());
} private HttpResponseMessage Execute()
{
HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK);
try
{
httpResponseMessage.Content = new StreamContent(_stream);
httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue(_mediaType);
if (!string.IsNullOrEmpty(_fileName))
{
httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = HttpUtility.UrlEncode(_fileName, Encoding.UTF8),
};
}
return httpResponseMessage;
}
catch
{
httpResponseMessage.Dispose();
throw;
}
}
}
使用方法:
[HttpGet]
public IHttpActionResult Logo()
{
var path = HostingEnvironment.MapPath("/files/logo.png");
var f = new FileInfo(path);
if (!f.Exists)
{
return new StatusCodeResult(HttpStatusCode.NotFound, this);
}
return new FileStreamResult(f.OpenRead(), "image/png");
} [HttpGet]
public IHttpActionResult D()
{
var path = HostingEnvironment.MapPath("/files/d.docx");
var f = new FileInfo(path);
if (!f.Exists)
{
return new StatusCodeResult(HttpStatusCode.NotFound, this);
}
return new FileStreamResult(f.OpenRead(), "application/octet-stream", "中文的jun2019.docx");
}
扩展的FileByteResult
public class FileByteResult : FileStreamResult
{
public FileByteResult(byte[] buffer, string mediaType) : base(new MemoryStream(buffer), mediaType) { } public FileByteResult(byte[] buffer, string mediaType, string fileName) : base(new MemoryStream(buffer), mediaType, fileName) { }
}
webapi返回文件流的更多相关文章
- Ajax异步请求返回文件流(eg:导出文件时,直接将导出数据用文件流的形式返回客户端供客户下载)
在异步请求中要返回文件流,不能使用JQuery,因为$.ajax,$.post 不支持返回二进制文件流的类型,可以看到下图,dataType只支持xml,json,script,html这几种格式,没 ...
- java 后台返回文件流到浏览器
package com.springbootblog.controller; import io.swagger.annotations.ApiImplicitParam;import io.swag ...
- 文件下载post请求,返回文件流转换zip,
最近一个需求是批量下载文件,最后打包成zip包,post请求, 因为是文件流下载,所以在取后台数据的时候,要多传递一个[responseType: ‘blob’]这个参数 download() { t ...
- 需要加token验证的接口返回文件流下载
没有加token之前,下载文件用的是a标签,直接下载. 现在要求是需要在header中加入token. getDownload(urls, fileName) { var url = urls; va ...
- c语言中的文件流
一.打开和关闭文件 #include int main( void ) { FILE* pReadFile = fopen( "E:\\mytest.txt", "r&q ...
- SpringMVC(Springboot)返回文件方法
https://blog.csdn.net/Lynn_coder/article/details/79953977 ****************************************** ...
- 【转载】C#.NET WebApi返回各种类型(图片/json数据/字符串),.net图片转二进制流或byte
C#.NET WebApi返回各种类型(图片/json数据/字符串),.net图片转二进制流或byte 转载:http://www.itdos.com/Mvc/20150302/0741255.htm ...
- C#.NET WebApi返回各种类型(图片/json数据/字符串),.net图片转二进制流或byte
using System.IO; /// <summary> /// WebApi返回图片 /// </summary> public HttpResponseMessage ...
- springboot 头像上传 文件流保存 文件流返回浏览器查看 区分操作系统 windows 7 or linux
//我的会员中心 头像上传接口 /*windows 调试*/ @Value("${appImg.location}") private String winPathPic; /*l ...
随机推荐
- c/c++ 动态库与静态库的制作和使用
静态库的用法 静态库的文件名 libxxx.a -->对应windows的.lib文件 做静态库的命令: ar rcs libxxx.a file1.o file2.o file.o 使用静态库 ...
- .NET 基金会完成第一次全面改选
.NET基金会是一个独立的组织,支持.NET社区和开源,旨在拓宽和加强.NET生态系统和社区.这可以通过多种方式完成,包括项目指导,指导,法律和营销帮助,技术和财务支持设置等,2014年微软组织成立. ...
- GBDT和XGBOOST算法原理
GBDT 以多分类问题为例介绍GBDT的算法,针对多分类问题,每次迭代都需要生成K个树(K为分类的个数),记为\(F_{mk}(x)\),其中m为迭代次数,k为分类. 针对每个训练样本,使用的损失函数 ...
- 基于IPV6数据包的分析(GNS3)
一.实验拓扑 二.路由配置 1.路由R1的详细配置(以R1为例,R2与R3相同) R1(config)#interface fastEthernet 0/1 R1(config-if)#ipv6 ad ...
- 由一个emoji引发的思考
由一个emoji引发的思考 从毕业以来,基本就一直在做移动端,但是一直就关于移动端的开发,各种适配问题的解决,在日常搬砖中处理了就过了,也没有把东西都沉淀下来,觉得甚是寒颜.现就一个小bug,让我们来 ...
- C# 《编写高质量代码改善建议》整理&笔记 --(四)资源管理&序列化
1.显示释放资源需继承接口IDisposable 什么是资源:C#中每一个类型都代表一种资源,而资源又分为以下两类. 托管资源:由CLR管理分配和释放的资源,即从CLR里new出来的对象. 非托管资源 ...
- SQL慢查询测试实践
1.开启慢查询的目的 开启慢查询日志,可以让MySQL记录下查询超过指定时间的语句,通过定位分析性能的瓶颈,才能更好的优化数据库系统的性能. 2.设置mysql慢查询 方法一:全局变量设置(临时生效) ...
- service相关
本篇仍以问题为驱动 一.什么时Service? Service是Android程序中四大基础组件之一,它和Activity一样都是Context的子类,只不过它没有UI界面,是在后台运行的组件.Ser ...
- ubuntu16.04无法获取ip地址的解决方案
当我们无法获取ip地址时可以使用dhcp来动态获取ip地址,安装dhcpcd5和dhcpcd-gtk sudo apt-get install dhcpcd5 sudo apt-get install ...
- Asp.Net Core WebAPI使用Swagger时API隐藏与分组
1.前言 为什么我们要隐藏部分接口? 因为我们在用swagger代替接口的时候,难免有些接口会直观的暴露出来,比如我们结合Consul一起使用的时候,会将健康检查接口以及报警通知接口暴露出来,这些接口 ...