当前位置: 首页 > news >正文

asp.net webapi实现FileStreamResult

问题

asp.net framework下的webapi没有FileStreamResult,如果想实现和mvc或者.net core下的文件返回需要自己封装实现

创建

新建FileStreamReusltContent.cs

using System.IO;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Net;
using System.Threading.Tasks;
using System.Threading;
using System.Web.Http;namespace FileDownload01.Result
{public class FileStreamReusltContent : IHttpActionResult{private readonly Stream _fileStream;private readonly string _contentType;private readonly string _fileDownloadName;public FileStreamReusltContent(Stream fileStream, string contentType, string fileDownloadName){_fileStream = fileStream;_contentType = contentType;_fileDownloadName = fileDownloadName;}public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken){var response = new HttpResponseMessage(HttpStatusCode.OK);var stream = _fileStream;response.Content = new StreamContent(stream);response.Content.Headers.ContentType = new MediaTypeHeaderValue(_contentType);response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"){FileName = _fileDownloadName};return Task.FromResult(response);}}
}

新建FileReusltContent .cs

using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;namespace FileDownload01.Result
{public class FileReusltContent : IHttpActionResult{private readonly string _filePath;private readonly string _contentType;private readonly string _fileDownloadName;public FileReusltContent(string filePath, string contentType, string fileDownloadName){_filePath = filePath;_contentType = contentType;_fileDownloadName = fileDownloadName;}public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken){var response = new HttpResponseMessage(HttpStatusCode.OK);var stream = new FileStream(_filePath, FileMode.Open, FileAccess.Read);response.Content = new StreamContent(stream);response.Content.Headers.ContentType = new MediaTypeHeaderValue(_contentType);response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"){FileName = _fileDownloadName};return Task.FromResult(response);}}
}

使用

[HttpGet]
public IHttpActionResult Get()
{string filePath = HttpContext.Current.Server.MapPath("~/pdf/001.pdf");string contentType = "application/pdf";string fileDownloadName = "001.pdf";//静态文件返回//return new FileReusltContent(filePath, contentType, fileDownloadName);//stream流返回var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);return new FileStreamReusltContent(fileStream, contentType, fileDownloadName);
}

http://www.mrgr.cn/news/61950.html

相关文章:

  • 等概率器的求解
  • 用python将pdf转成图片转换成对应的word文件
  • Python自动化测试一文详解
  • 【后台管理系统】
  • 算法练习:LCR 179. 查找总价格为目标值的两个商品
  • Java三大特性之继承
  • Maven下载安装配置(环境、本地仓库、阿里云、jdk、idea)(Win11)
  • 论分布式存储系统架构设计
  • 为什么资产管理中会用到RFID系统
  • NavVis LX系列产品典型应用—现有住宅装修改造-沪敖3D
  • 使用SPM为ios项目添加lookin所遇问题总结
  • 记MySQL下一次DEPENDENT SUBQUERY的优化
  • 从0学习React(10)
  • 代码随想录算法训练营第三十一天|Day31 贪心算法
  • 【PG高可用】patroni配置文件
  • 怎样禁止运行电脑某个软件(如何禁止运行电脑软件)?3分钟学会这4招!
  • Educational Codeforces Round 171
  • OBC充电机测试性能评估
  • Java面试经典 150 题.P88. 合并两个有序数组(001)
  • 【C++】string 类深度解析:探秘字符串操作的核心
  • python公寓出租管理系统-计算机毕业设计源码00130
  • 项目开发的架构模式与异步请求(AJAX)
  • 香橙派Orangepi 5plus 配置Hailo-8/Hailo-8L
  • 2024 Rust现代实用教程:1.2编译器与包管理工具以及开发环境搭建
  • 荒野大镖客:救赎 PC版整合包
  • 【K8S系列】Kubernetes 中 NodePort 类型的 Service 无法访问的问题【已解决】