1、以文件流下载
       byte[] fileStr=new  byte[5];

            MemoryStream btMs = new MemoryStream(fileStr);

            //以字符流的形式下载文件 

            byte[] bytes = new byte[(int)btMs.Length];
btMs.Read(bytes, 0, bytes.Length);
btMs.Close();
Page.Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
Page.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
Page.Response.BinaryWrite(bytes);
Page.Response.Flush();
Page.Response.End();

2、根据文件路径下载

public void Download(string path)
{ string filePath = HttpContext.Current.Server.MapPath(path);//路径 //以字符流的形式下载文件
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
HttpContext.Current.Response.ContentType = "application/octet-stream"; HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("test", System.Text.Encoding.UTF8));
HttpContext.Current.Response.BinaryWrite(bytes);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}

3.直接下载

WebClient client = new WebClient();

//文件存放路径
var path = HttpContext.Current.Server.MapPath(@"\Static\BtDownload\");

//判断文件是否存在
var file = new FileInfo(path + filename);
if (!file.Exists)
{
 client.DownloadFile(value, path + filename);
}

最新文章

  1. iOS 限制TextField输入长度(标准)
  2. Android 上传图片并添加参数 PHP接收
  3. Phonegap中自定义插件的使用
  4. html中相似的标签、属性的区别
  5. linux 常用目录
  6. struts2--表单重复提交
  7. mac下phpstorm左侧的project列表找不到了
  8. P1082 找朋友
  9. CM 部署bigdata测试环境群集机器报错
  10. [LeetCode]题解(python):115-Distinct Subsequences
  11. 记得12306货运系统“抢购空”编写插件--chrome交互式插件的各个部分
  12. QQ头像一键添加校徽
  13. sqoop关系型数据迁移原理以及map端内存为何不会爆掉窥探
  14. Vim编辑器的注释,解注,删除与恢复
  15. Ubuntu18.04安装netstat
  16. JVM核心知识体系(转http://www.cnblogs.com/wxdlut/p/10670871.html)
  17. SpringBoot 使用validation数据校验
  18. webpack 相关插件及作用(表格)
  19. MacBook PRO蓝牙无法搜索设备
  20. Swing开发图形界面有如下优势

热门文章

  1. svn提交时强制注释
  2. CSS---网络编程
  3. Bzoj 2763: [JLOI2011]飞行路线 dijkstra,堆,最短路,分层图
  4. xls 和 xml 数据 排序 绑定 -原创
  5. Uber能知道你是不是在开车的时候玩手机
  6. sql中时间的一些特殊转换
  7. array_column php 函数
  8. Jsp学习(2)
  9. DocX组件读取与写入Word
  10. 【转】java 解析 plist文件