

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