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中配置附件要上传的 ...
随机推荐
- 摄像头调用,h5调用摄像头进行扫一扫插件备份
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- AJPFX关于面向对象之封装,继承,多态 (下)
(3)private: 对于对于成员来说:只能在该成员隶属于的类中访问. 对于类来说:类不可以声明为private. 4)protected: 对于对于成员来说:相同包中的类可以访问(包访问权限):基 ...
- 新建cordova应用,插件开发教程系列(总目录)
以下几篇是连续的教程,代码也是连续的,包括如下章节: 新建cordova应用 https://www.cnblogs.com/cannel/p/11074359.html 使用cordova把h5应用 ...
- 人物丨让小三吐血,让原配泣血——24K渣男郎咸平
http://url.cn/5swgmythttps://www.toutiao.com/i6650650793743483395人物丨让小三吐血,让原配泣血——24K渣男郎咸平 人物丨让小三吐血,让 ...
- 洛谷 P2153 [SDOI2009]晨跑
题目描述 Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑.仰卧起坐等 等,不过到目前为止,他坚持下来的只有晨跑. 现在给出一张学校附近的地图,这张地图中包含N个十字路口和M条街 ...
- $.noconflict() 有什么用处
jQuery默认使用"$"操作符,prototype等其他框架也是是使用"$",于是,如果jQuery在其他库之后引入,那么jQuery将获得"$&q ...
- JS怎么创建一个类?
15. JS怎么创建一个类? 方式1 : var obj = new Object(); 方式2 : var obj = {}; 16.JS的typeof返回哪些数据类型? string.number ...
- Format a Hard Drive in Csharp
Article Author(s): Audric Thevenet All Rights Reserved. Here's how to format hard drives, floppies, ...
- ZOJ3228 Searching the String (AC自动机)
Searching the String Time Limit: 7 Seconds Memory Limit: 129872 ...
- WebDriverException: Message: unknown error: Chrome failed to start: crashed
the last answer WebDriverException: Message: unknown error: Chrome failed to start: crashed