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