WebApi下载附件文件
WebApi下载附件文件
1.
[RoutePrefix("down")]
public class FilesController : ApiController
{
[GET("file/{id}")]
public HttpResponseMessage GetSomePdf(string id)
{
var path = MyApp.PathToSomePdf(id);
if (path!= null)
return FileAsAttachment(path, "somepdf.pdf");
return new HttpResponseMessage(HttpStatusCode.NotFound);
}
public static HttpResponseMessage FileAsAttachment(string path, string filename)
{
if (File.Exists(path))
{
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
var stream = new FileStream(path, FileMode.Open);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = filename;
return result;
}
return new HttpResponseMessage(HttpStatusCode.NotFound);
}
2.
public class DataController : ApiController
{
// Sample content used to demonstrate range requests
private static readonly byte[] _content = File.ReadAllBytes(HttpContext.Current.Server.MapPath("~/Content/airports.csv")); // Content type for our body
private static readonly MediaTypeHeaderValue _mediaType = MediaTypeHeaderValue.Parse("text/csv"); public HttpResponseMessage Get(bool IsLengthOnly)
{
//if only length is requested
if (IsLengthOnly)
{
return Request.CreateResponse(HttpStatusCode.OK, _content.Length);
}
else
{
MemoryStream memStream = new MemoryStream(_content); // Check to see if this is a range request (i.e. contains a Range header field)
if (Request.Headers.Range != null)
{
try
{
HttpResponseMessage partialResponse = Request.CreateResponse(HttpStatusCode.PartialContent);
partialResponse.Content = new ByteRangeStreamContent(memStream, Request.Headers.Range, _mediaType);
return partialResponse;
}
catch (InvalidByteRangeException invalidByteRangeException)
{
return Request.CreateErrorResponse(invalidByteRangeException);
}
}
else
{
// If it is not a range request we just send the whole thing as normal
HttpResponseMessage fullResponse = Request.CreateResponse(HttpStatusCode.OK);
fullResponse.Content = new StreamContent(memStream);
fullResponse.Content.Headers.ContentType = _mediaType;
return fullResponse;
}
}
}
}
WebApi下载附件文件的更多相关文章
- webapi 下载Ftp文件并返回流到浏览器完成文件下载
ResultModel<HttpResponseMessage> resultModel = new ResultModel<HttpResponseMessage>(Resu ...
- java 通过sftp服务器上传下载删除文件
最近做了一个sftp服务器文件下载的功能,mark一下: 首先是一个SftpClientUtil 类,封装了对sftp服务器文件上传.下载.删除的方法 import java.io.File; imp ...
- 在Winform开发框架中对附件文件进行集中归档处理
在我们Winform开发中,往往需要涉及到附件的统一管理,因此我倾向于把它们独立出来作为一个附件管理模块,这样各个模块都可以使用这个附件管理模块,更好的实现模块重用的目的.在涉及附件管理的场景中,一个 ...
- Nginx 配置下载附件让浏览器提示用户是否保存
Nginx配置下载附件让浏览器提示用户是否保存 by:授客 QQ:1033553122 测试环境 nginx-1.10.0 问题描述: 前端页面,IE11浏览器下请求下载附件模板,针对xls ...
- C#下载歌词文件
前段时间写了一篇c#解析Lrc歌词文件,对lrc文件进行解析,支持多个时间段合并.本文借下载歌词文件来探讨一下同步和异步方法. Lrc文件在网络上随处可见,我们可以通过一些方法获取,最简单的就是别人的 ...
- PHP下载APK文件
PHP下载APK文件(代码如下) /** * //这里不要随便打印文字,否则会影响输出的文件的 * (例如下载没问题,但是apk安装时候提醒解析安装包错误) * @return array */ pu ...
- mac 火狐 下载 任何文件都是失败
今天在从邮件中下载附件,怎么点击下载 浏览器上都是失败 最后突然想到,我改过火狐的下载路径,改到根目录下了,根目录下应该是没有权限保存文件的 后把下载路径改成 “下载” 就可以正常下载了
- java上传附件,批量下载附件(一)
上传附件代码:借助commons-fileupload-1.2.jar package com.str; import java.io.BufferedInputStream;import java. ...
- vue+springboot上传和下载附件功能
https://blog.csdn.net/qq_35867245/article/details/84325385 上传附件(服务端代码) 第一步:在application.yml中配置附件要上传的 ...
随机推荐
- python2和python3的区别(转)
基本语法差异 核心类差异 Python3对Unicode字符的原生支持 Python2中使用 ASCII 码作为默认编码方式导致string有两种类型str和unicode,Python3只支持uni ...
- How to proxy a web site by apache2 in Ubuntu
Install apache2 To execute the install command in terminal: sudo apt-get install apache2 Then, we ca ...
- 用户授权policy
定义策略类 php artisan make:policy PostPolicy app/Policies/PostPolicy.php public function update(User $us ...
- laravel composer 扩展包开发(超详细)
laravel composer 扩展包开发(超详细) 置顶 2018年02月05日 11:09:16 Simael__Aex 阅读数:10396 版权声明:转载请注明出处:http://blo ...
- $.noconflict() 有什么用处
jQuery默认使用"$"操作符,prototype等其他框架也是是使用"$",于是,如果jQuery在其他库之后引入,那么jQuery将获得"$&q ...
- JS怎么创建一个类?
15. JS怎么创建一个类? 方式1 : var obj = new Object(); 方式2 : var obj = {}; 16.JS的typeof返回哪些数据类型? string.number ...
- QT+常见控件+tab Widget 和Stacked Widget
首先:这里介绍以下tab Widget 和Stacked Widget 之间的区别和使用的方法: tab Widget控件可以直接的进行切换,Stacked Widget却不可以直接在界面上进行切换, ...
- Xcode导入第三方库
Xcode导入第三方库,例如TapkuLibrary iOS开源框架Tapku下载地址:https://github.com/devinross/tapkulibrary.git 1.创建你的工程项目 ...
- WebSocket 学习笔记
WebSocket 学习笔记 来自我的博客 因为项目原因需要用到双工通信,所以比较详细的学习了一下浏览器端支持的 WebSocket. 并记录一些遇到的问题. 简介 WebSocket 一般是指浏览器 ...
- Mysql数据库事件使用与示例
1 事件简介 事件(event)是MySQL在相应的时刻调用的过程式数据库对象.一个事件可调用一次,也可周期性的启动,它由一个特定的线程来管理的,也就是所谓的“事件调度器”. 事件和触发器类似,都是在 ...