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中配置附件要上传的 ...
随机推荐
- SpringBoot 2.x (8):模板引擎
SpringBoot中有很多的starter:本质是多个JAR包集合 比如我们常用的: <dependency> <groupId>org.springframework.bo ...
- ios-获取系统相簿里边的所有照片
#import<AssetsLibrary/AssetsLibrary.h> -(void)getImgs{ dispatch_async(dispatch_get_main_queue( ...
- mongodb的安装及配置安装服务
1. 安装mongodb数据库 mongodb官方网址:https://www.mongodb.org 安装好之后的步奏: 第一步:规划你的安装目录和数据库文件的存储路径,我打算将Mongo的程序文件 ...
- Nagios的服务器监控
第一部分是主机外监控,比如:主机是否存活,WEB服务是否正常,MySQL服务是否正常等内容,再主机外通过访问其端口即可得知.这些监控命令再安装nagios-plugins-1.4.13.tar.gz时 ...
- RHEL5.8上SAMBA源码修改打包安装流程
之前一直使用系统自带的SAMBA,近期需要对SAMBA代码做一些修改,然后还是打算用RPM包的方式来安装部署. 这个流程本身不复杂,在这里记录下来,免得在另外写说明文档. 关键词:RHEL5.8, s ...
- codevs 1979 第K个数
时间限制: 1 s 空间限制: 1000 KB 题目等级 : 黄金 Gold 题目描述 Description 给定一个长度为N(0<n<=10000)的序列,保证每一个序列中的数字 ...
- InvocationTargetException异常的深入研究-servlet的setAttribute与getAttribute
在某项目中,前端jsp传入的用户id数据通过session域传入后台servlet进行处理的过程中,无意间出现了InvocationTargetException异常 前端部分代码如下:测试代码,非原 ...
- 如何安装Virtual Box的VBox Guest Additions扩展程序
Virtual Box的默认安装是不包含Guest Addition这个扩展的,在实际使用过程中带来种种不便,比如只能通过小窗口访问虚拟机的操作系统,通过默认的右Ctrl切换鼠标,不能和宿主操作系统共 ...
- Android(java)学习笔记178:多媒体之计算机图形表示方式
1. 多媒体 很多媒体:文字(TextView,简单不讲),图片,声音,视频等等. 2. 图片 计算机如何表示图片的? (1)bmp 高质量保存 256色位图:图片中的每个像素点可以有256 ...
- QT +菜单栏和工具栏
#include "mainwindow.h" #include <QMenuBar>//菜单栏需要的头文件 #include <QMenu>//菜单 #i ...