api下载文件
net
- /// <summary>
- ///字符流下载方法
- /// </summary>
- /// <param name="fileName">下载文件生成的名称</param>
- /// <param name="fPath">下载文件路径</param>
- /// <returns></returns>
- public void DownloadFile(string fileName, string fPath)
- {
- string filePath = Server.MapPath(fPath);//路径
- //以字符流的形式下载文件
- FileStream fs = new FileStream(filePath, FileMode.Open);
- byte[] bytes = new byte[(int)fs.Length];
- fs.Read(bytes, , bytes.Length);
- fs.Close();
- Response.ContentType = "application/octet-stream";
- //通知浏览器下载文件而不是打开
- Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
- // 添加头信息,指定文件大小,让浏览器能够显示下载进度
- Response.AddHeader("Content-Length", file.Length.ToString());
- Response.BinaryWrite(bytes);
- Response.Flush();
- Response.End();
- }
字符流下载方法
WriteFile实现下载
- string fileName ="aaa.zip";//客户端保存的文件名
- string filePath=Server.MapPath("DownLoad/aaa.zip");//路径
- FileInfo fileInfo = new FileInfo(filePath);
- Response.Clear();
- Response.ClearContent();
- Response.ClearHeaders();
- Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
- Response.AddHeader("Content-Length", fileInfo.Length.ToString());
- Response.AddHeader("Content-Transfer-Encoding", "binary");
- Response.ContentType = "application/octet-stream";
- Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
- Response.WriteFile(fileInfo.FullName);
- Response.Flush();
- Response.End();
- Response.TransmitFile(filePath);
WriteFile分块下载
- string fileName = "aaa.zip";//客户端保存的文件名
- string filePath = Server.MapPath("DownLoad/aaa.zip");//路径
- System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
- if (fileInfo.Exists == true)
- {
- const long ChunkSize = ;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
- byte[] buffer = new byte[ChunkSize];
- Response.Clear();
- System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
- long dataLengthToRead = iStream.Length;//获取下载的文件总大小
- Response.ContentType = "application/octet-stream";
- Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
- while (dataLengthToRead > && Response.IsClientConnected)
- {
- int lengthRead = iStream.Read(buffer, , Convert.ToInt32(ChunkSize));//读取的大小
- Response.OutputStream.Write(buffer, , lengthRead);
- Response.Flush();
- dataLengthToRead = dataLengthToRead - lengthRead;
- }
- Response.Close();
- }
WEBAPI
- public HttpResponseMessage DownloadByPath(string filePath)
- {
- try
- {
- if (!File.Exists(filePath))
- {
- filePath = MapPath(filePath);
- if (!File.Exists(filePath))
- {
- throw new HttpResponseException(HttpStatusCode.NotFound);
- }
- }
- FileStream fileStream = new FileStream(filePath, FileMode.Open);
- HttpResponseMessage fullResponse = Request.CreateResponse(HttpStatusCode.OK);
- fullResponse.Content = new StreamContent(fileStream);
- MediaTypeHeaderValue _mediaType = MediaTypeHeaderValue.Parse("application/octet-stream");//指定文件类型
- ContentDispositionHeaderValue _disposition = ContentDispositionHeaderValue.Parse("attachment;filename=" + System.Web.HttpUtility.UrlEncode(Path.GetFileName(filePath)));//指定文件名称 System.Web.HttpUtility.UrlEncode 不加失败
- fullResponse.Content.Headers.ContentType = _mediaType;
- fullResponse.Content.Headers.ContentDisposition = _disposition;
- return fullResponse;
- }
- catch (Exception ex)
- {
- Logger.Error(ex);
- throw new HttpResponseException(HttpStatusCode.NotFound);
- }
- }
System.Web.HttpUtility.UrlEncode 出错更改
- fullResponse.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = docName };
- var url = API接口;
- let link = document.createElement('a');
- link.href = url;
- link.target = "_blank";
- link.click();
- window.URL.revokeObjectURL(link.href);
js
API 传输数据加密
api下载文件的更多相关文章
- Meteor 前端 RESTful API 通过后端 API 下载文件
Meteor 下载文件 问题场景 后端 HTTP server提供一个下载接口,可是须要前端 Meteor 可以给浏览器用户开一个URL来下载这个文件. 举例:在线的Meteor Logo文件就好比后 ...
- Asp.Net Web Api 2 实现多文件打包并下载文件示例源码_转
一篇关于Asp.Net Web Api下载文件的文章,之前我也写过类似的文章,请见:<ASP.NET(C#) Web Api通过文件流下载文件到本地实例>本文以这篇文章的基础,提供了Byt ...
- 调用百度云Api实现从百度云盘自动下载文件
一.注册账号 要从百度云下载文件,首先,注册一个百度云账号,现在可能都要注册手机号啦,当然,如果你已经注册过,很幸运,就可以省略掉此步骤啦. 如图登录后所示: 点击Access Key,即显示上面的图 ...
- rest api上传和下载文件
rest api上传和下载文件 function FileToString(AFileName: string): string; var LMemoryStream: TMemoryStream; ...
- ExtJs 6.0+快速入门,ext-bootstrap.js文件的分析,各版本API下载
ExtJS6.0+快速入门+API下载地址 ExtAPI 下载地址如下,包含各个版本 http://docs.sencha.com/misc/guides/offline_docs.html 1.使用 ...
- Web API 上传下载文件
1.引用了一个第三方组件 ICSharpCode.SharpZipLib.Zip; 2.具体代码 实体类,可以用hashtable 替代 ,感觉hashtable 比较灵活 public class ...
- 转:python webdriver API 之下载文件
webdriver 允许我们设置默认的文件下载路径.也就是说文件会自动下载并且存在设置的那个目录中.要想下载文件,首选要先确定你所要下载的文件的类型.要识别自动文件的下载类型可以使用 curl ,如图 ...
- 使用java原生API模拟请求下载文件
/** * * @param urlPath * 下载路径 * @param saveDir * 下载存放目录 * @return 返回下载文件 * @throws Exception */ publ ...
- 通过form表单的形式下载文件。
在项目中遇到问题,要求动态拼接uri下载文件.但是由于项目的安全拦截导致window.location.href 和 window.open等新建窗口的方法都不行. 无意间百度到了通过form表单来下 ...
随机推荐
- 使用SQL Delta.v5.1.1.98.破解版同步数据结构
概述 本篇文章主要介绍SQL DELTA的简单使用.为了能够更加明了的说明其功能,本文将通过实际项目中的案例加以介绍. 1. SQLDELTA简介 SQLDELTA是一款便捷实用的数据库管理工具.使用 ...
- mysql --initialize specified but the data directory has files in it
删除 *.ini 文件中的datadir=“....”目录下的文件,即可.
- python3 的zip()函数
重点 https://blog.csdn.net/qq826364410/article/details/78259796 为啥会出现几个两个空列表????
- [Objective-C语言教程]循环语句(9)
当需要多次执行同一代码块时,可以使用循环来解决. 通常,语句按顺序执行:首先执行函数中的第一个语句,然后执行第二个语句,依此类推. 编程语言提供各种控制结构,允许更复杂的执行路径.循环语句可用于多次执 ...
- Archlinux 下系统如何设置让 Wine 调用 ibus输入法
前言: 如果你是fcitx输入法用户,那么这篇文章大可不必看.fcitx是一个非常强大的框架,著名搜狗输入法就是基于fcitx输入法架构开发的.据我所知.您遇到这个问题可以通过卸载ibus输入法进行修 ...
- python 学习(pip工具的安装)
mac 电脑上使用终端命令 curl https://bootstrap.pypa.io/get-pip.py | python3 基于Python 3 pip --version pip3 list ...
- 01Trie树 CF923C Perfect Security
CF923C Perfect Security 上下各n个数,求一种排列p,使上面的数i异或pi成为新的数i,求方案另字典序最小,输出该结果 01Trie树. 记录每个节点经过多少次. 每一次查询的时 ...
- js 平均分割
let alllist=res.data; var result = []; for (var i = 0; i < alllist.length; i += 3) { result.push( ...
- 扩大VirtualBox虚拟机磁盘的方法
之前在VirtualBox里安装了一个XP系统,当时只分配了10G磁盘空间,随着使用,空间不足了. 在虚拟机管理器里不能直接调整磁盘的大小,这里要用到VirtualBox安装目录下的VBoxManag ...
- HDU - 5306 剪枝的线段树
题意:给定\(a[1...n]\),\(m\)次操作,0表示使\([L,R]\)中的值\(a[i]=min(a[i],x)\),其余的1是查最值2是查区间和 本题是吉利爷的2016论文题,1 2套路不 ...