net

  1. /// <summary>
  2. ///字符流下载方法
  3. /// </summary>
  4. /// <param name="fileName">下载文件生成的名称</param>
  5. /// <param name="fPath">下载文件路径</param>
  6. /// <returns></returns>
  7. public void DownloadFile(string fileName, string fPath)
  8. {
  9. string filePath = Server.MapPath(fPath);//路径
  10. //以字符流的形式下载文件
  11. FileStream fs = new FileStream(filePath, FileMode.Open);
  12. byte[] bytes = new byte[(int)fs.Length];
  13. fs.Read(bytes, , bytes.Length);
  14. fs.Close();
  15. Response.ContentType = "application/octet-stream";
  16. //通知浏览器下载文件而不是打开
  17. Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
  18. // 添加头信息,指定文件大小,让浏览器能够显示下载进度
  19. Response.AddHeader("Content-Length", file.Length.ToString());
  20.  
  21. Response.BinaryWrite(bytes);
  22. Response.Flush();
  23. Response.End();
  24. }

字符流下载方法

WriteFile实现下载

  1. string fileName ="aaa.zip";//客户端保存的文件名
  2. string filePath=Server.MapPath("DownLoad/aaa.zip");//路径
  3.  
  4. FileInfo fileInfo = new FileInfo(filePath);
  5. Response.Clear();
  6. Response.ClearContent();
  7. Response.ClearHeaders();
  8. Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
  9. Response.AddHeader("Content-Length", fileInfo.Length.ToString());
  10. Response.AddHeader("Content-Transfer-Encoding", "binary");
  11. Response.ContentType = "application/octet-stream";
  12. Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
  13. Response.WriteFile(fileInfo.FullName);
  14. Response.Flush();
  15. Response.End();
  1. Response.TransmitFile(filePath);

WriteFile分块下载

  1. string fileName = "aaa.zip";//客户端保存的文件名
  2. string filePath = Server.MapPath("DownLoad/aaa.zip");//路径
  3.  
  4. System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
  5.  
  6. if (fileInfo.Exists == true)
  7. {
  8. const long ChunkSize = ;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
  9. byte[] buffer = new byte[ChunkSize];
  10.  
  11. Response.Clear();
  12. System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
  13. long dataLengthToRead = iStream.Length;//获取下载的文件总大小
  14. Response.ContentType = "application/octet-stream";
  15. Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
  16. while (dataLengthToRead > && Response.IsClientConnected)
  17. {
  18. int lengthRead = iStream.Read(buffer, , Convert.ToInt32(ChunkSize));//读取的大小
  19. Response.OutputStream.Write(buffer, , lengthRead);
  20. Response.Flush();
  21. dataLengthToRead = dataLengthToRead - lengthRead;
  22. }
  23. Response.Close();
  24. }

WEBAPI

  1. public HttpResponseMessage DownloadByPath(string filePath)
  2. {
  3. try
  4. {
  5. if (!File.Exists(filePath))
  6. {
  7. filePath = MapPath(filePath);
  8. if (!File.Exists(filePath))
  9. {
  10. throw new HttpResponseException(HttpStatusCode.NotFound);
  11. }
  12. }
  13.  
  14. FileStream fileStream = new FileStream(filePath, FileMode.Open);
  15. HttpResponseMessage fullResponse = Request.CreateResponse(HttpStatusCode.OK);
  16. fullResponse.Content = new StreamContent(fileStream);
  17.  
  18. MediaTypeHeaderValue _mediaType = MediaTypeHeaderValue.Parse("application/octet-stream");//指定文件类型
  19. ContentDispositionHeaderValue _disposition = ContentDispositionHeaderValue.Parse("attachment;filename=" + System.Web.HttpUtility.UrlEncode(Path.GetFileName(filePath)));//指定文件名称 System.Web.HttpUtility.UrlEncode 不加失败
  20.  
  21. fullResponse.Content.Headers.ContentType = _mediaType;
  22. fullResponse.Content.Headers.ContentDisposition = _disposition;
  23. return fullResponse;
  24. }
  25. catch (Exception ex)
  26. {
  27. Logger.Error(ex);
  28. throw new HttpResponseException(HttpStatusCode.NotFound);
  29. }
  30. }

System.Web.HttpUtility.UrlEncode 出错更改

  1. fullResponse.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = docName };
  1. var url = API接口;
  2. let link = document.createElement('a');
  3. link.href = url;
  4. link.target = "_blank";
  5. link.click();
  6. window.URL.revokeObjectURL(link.href);

js

API 传输数据加密

api下载文件的更多相关文章

  1. Meteor 前端 RESTful API 通过后端 API 下载文件

    Meteor 下载文件 问题场景 后端 HTTP server提供一个下载接口,可是须要前端 Meteor 可以给浏览器用户开一个URL来下载这个文件. 举例:在线的Meteor Logo文件就好比后 ...

  2. Asp.Net Web Api 2 实现多文件打包并下载文件示例源码_转

    一篇关于Asp.Net Web Api下载文件的文章,之前我也写过类似的文章,请见:<ASP.NET(C#) Web Api通过文件流下载文件到本地实例>本文以这篇文章的基础,提供了Byt ...

  3. 调用百度云Api实现从百度云盘自动下载文件

    一.注册账号 要从百度云下载文件,首先,注册一个百度云账号,现在可能都要注册手机号啦,当然,如果你已经注册过,很幸运,就可以省略掉此步骤啦. 如图登录后所示: 点击Access Key,即显示上面的图 ...

  4. rest api上传和下载文件

    rest api上传和下载文件 function FileToString(AFileName: string): string; var LMemoryStream: TMemoryStream; ...

  5. ExtJs 6.0+快速入门,ext-bootstrap.js文件的分析,各版本API下载

    ExtJS6.0+快速入门+API下载地址 ExtAPI 下载地址如下,包含各个版本 http://docs.sencha.com/misc/guides/offline_docs.html 1.使用 ...

  6. Web API 上传下载文件

    1.引用了一个第三方组件 ICSharpCode.SharpZipLib.Zip; 2.具体代码 实体类,可以用hashtable 替代 ,感觉hashtable 比较灵活 public class ...

  7. 转:python webdriver API 之下载文件

    webdriver 允许我们设置默认的文件下载路径.也就是说文件会自动下载并且存在设置的那个目录中.要想下载文件,首选要先确定你所要下载的文件的类型.要识别自动文件的下载类型可以使用 curl ,如图 ...

  8. 使用java原生API模拟请求下载文件

    /** * * @param urlPath * 下载路径 * @param saveDir * 下载存放目录 * @return 返回下载文件 * @throws Exception */ publ ...

  9. 通过form表单的形式下载文件。

    在项目中遇到问题,要求动态拼接uri下载文件.但是由于项目的安全拦截导致window.location.href 和 window.open等新建窗口的方法都不行. 无意间百度到了通过form表单来下 ...

随机推荐

  1. 使用SQL Delta.v5.1.1.98.破解版同步数据结构

    概述 本篇文章主要介绍SQL DELTA的简单使用.为了能够更加明了的说明其功能,本文将通过实际项目中的案例加以介绍. 1. SQLDELTA简介 SQLDELTA是一款便捷实用的数据库管理工具.使用 ...

  2. mysql --initialize specified but the data directory has files in it

    删除 *.ini 文件中的datadir=“....”目录下的文件,即可.

  3. python3 的zip()函数

    重点 https://blog.csdn.net/qq826364410/article/details/78259796 为啥会出现几个两个空列表????

  4. [Objective-C语言教程]循环语句(9)

    当需要多次执行同一代码块时,可以使用循环来解决. 通常,语句按顺序执行:首先执行函数中的第一个语句,然后执行第二个语句,依此类推. 编程语言提供各种控制结构,允许更复杂的执行路径.循环语句可用于多次执 ...

  5. Archlinux 下系统如何设置让 Wine 调用 ibus输入法

    前言: 如果你是fcitx输入法用户,那么这篇文章大可不必看.fcitx是一个非常强大的框架,著名搜狗输入法就是基于fcitx输入法架构开发的.据我所知.您遇到这个问题可以通过卸载ibus输入法进行修 ...

  6. python 学习(pip工具的安装)

    mac 电脑上使用终端命令 curl https://bootstrap.pypa.io/get-pip.py | python3 基于Python 3 pip --version pip3 list ...

  7. 01Trie树 CF923C Perfect Security

    CF923C Perfect Security 上下各n个数,求一种排列p,使上面的数i异或pi成为新的数i,求方案另字典序最小,输出该结果 01Trie树. 记录每个节点经过多少次. 每一次查询的时 ...

  8. js 平均分割

    let alllist=res.data; var result = []; for (var i = 0; i < alllist.length; i += 3) { result.push( ...

  9. 扩大VirtualBox虚拟机磁盘的方法

    之前在VirtualBox里安装了一个XP系统,当时只分配了10G磁盘空间,随着使用,空间不足了. 在虚拟机管理器里不能直接调整磁盘的大小,这里要用到VirtualBox安装目录下的VBoxManag ...

  10. HDU - 5306 剪枝的线段树

    题意:给定\(a[1...n]\),\(m\)次操作,0表示使\([L,R]\)中的值\(a[i]=min(a[i],x)\),其余的1是查最值2是查区间和 本题是吉利爷的2016论文题,1 2套路不 ...