asp.net下载文件的常用方法

2013 年 8 月 25 日3330

IT专家网 > winsystem

asp.net下载文件的常用方法

以下是代码片段:
1 //TransmitFile实现下载
2 protected void Button1_Click1(object sender, EventArgs e)
3 {
4
5 string strFileName = "三部闲置设备管理系统操作手册IEMS.ppt";
6 Response.ContentType = "application/x-zip-compressed";
7 //Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
8 string filename = BLL.Config.PART_EM_UPLOAD_DOC + strFileName;
9
10 //BLL.Config.PART_EM_UPLOAD_DOC 为路径 ("D:/EMUploadDoc/")
11 Response.AddHeader("Content-Disposition", "attachment;filename=" +Server.UrlPathEncode(strFileName));
12
13 //Server.UrlPathEncode()解决文件名的乱码问题.
14
15 Response.TransmitFile(filename);
16 } //WriteFile实现下载
17 protected void Button2_Click(object sender, EventArgs e)
18 {
19
20
21 string fileName = "asd.txt";//客户端保存的文件名
22 string filePath = Server.MapPath("DownLoad/aaa.txt");//路径
23
24 FileInfo fileInfo = new FileInfo(filePath);
25 Response.Clear();
26 Response.ClearContent();
27 Response.ClearHeaders();
28 Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
29 Response.AddHeader("Content-Length", fileInfo.Length.ToString());
30 Response.AddHeader("Content-Transfer-Encoding", "binary");
31 Response.ContentType = "application/octet-stream";
32 Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
33 Response.WriteFile(fileInfo.FullName);
34 Response.Flush();
35 Response.End();
36 }
37
38 //WriteFile分块下载
39 protected void Button3_Click(object sender, EventArgs e)
40 {
41
42 string fileName = "aaa.txt";//客户端保存的文件名
43 string filePath = Server.MapPath("DownLoad/aaa.txt");//路径
44
45 System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
46
47 if (fileInfo.Exists == true)
48 {
49 const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
50 byte[] buffer = new byte[ChunkSize];
51
52 Response.Clear();
53 System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
54 long dataLengthToRead = iStream.Length;//获取下载的文件总大小
55 Response.ContentType = "application/octet-stream";
56 Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
57 while (dataLengthToRead > 0 && Response.IsClientConnected)
58 {
59 int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小
60 Response.OutputStream.Write(buffer, 0, lengthRead);
61 Response.Flush();
62 dataLengthToRead = dataLengthToRead - lengthRead;
63 }
64 Response.Close();
65 }
66 }
67
68 //流方式下载
69 protected void Button4_Click(object sender, EventArgs e)
70 {
71 string fileName = "aaa.txt";//客户端保存的文件名
72 string filePath = Server.MapPath("DownLoad/aaa.txt");//路径
73
74 //以字符流的形式下载文件
75 FileStream fs = new FileStream(filePath, FileMode.Open);
76 byte[] bytes = new byte[(int)fs.Length];
77 fs.Read(bytes, 0, bytes.Length);
78 fs.Close();
79 Response.ContentType = "application/octet-stream";
80 //通知浏览器下载文件而不是打开
81 Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
82 Response.BinaryWrite(bytes);
83 Response.Flush();
84 Response.End();
85
86 }

相关文章

关键词:asp.net,.NET,微软技术

责任编辑:失落的羽毛

专题推荐

原创文章

微博互动

白皮书

All Rights Reserved, Copyright 2004-2013, Ctocio.com.cn
渝ICP证B2-20030003号 如有意见请与我们联系 powered by 天极内容管理平台CMS4i

0 0