文件下载2.0(简单代码)


 

 1None.gif
 2None.gifprotected void  Button1_Click(object sender, EventArgs e)
 3ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 4InBlock.gif
 5InBlock.gif    //*************文件的下载的实现**************
 6InBlock.gif
 7InBlock.gif
 8InBlock.gif     string path = Server.MapPath("~/image/此情可待.wma");
 9InBlock.gif           
10InBlock.gif //string path1 = Server.MapPath("~/image/PabloCastro.jpg");
11InBlock.gif
12InBlock.gif             
13InBlock.gif//string path = Server.MapPath("~/image/PabloCastro.jpg");
14InBlock.gif            System.IO.FileInfo file = new System.IO.FileInfo(path);
15InBlock.gif            string filename = file.Name.ToString();
16InBlock.gif            
17InBlock.gif// clear the current output content from the buffer
18InBlock.gif            
19InBlock.gifResponse.Clear();
20InBlock.gif            
21InBlock.gif// add the header that specifies the default filename for the Download/SaveAs dialog
22InBlock.gif           
23InBlock.gif Response.AppendHeader("Content-Disposition""attachment; filename=\"" + Server.UrlEncode(filename) + "\"");//ture解决名字乱码现象
24InBlock.gif
25InBlock.gif           
26InBlock.gif //Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);//乱码
27InBlock.gif            
28InBlock.gif// add the header that specifies the file size, so that the browser
29InBlock.gif            
30InBlock.gif// can show the download progress
31InBlock.gif            
32InBlock.gifResponse.AddHeader("Content-Length", file.Length.ToString());
33InBlock.gif            
34InBlock.gif// specify that the response is a stream that cannot be read by the
35InBlock.gif            
36InBlock.gif// client and must be downloaded
37InBlock.gif            Response.ContentType = "application/octet-stream";
38InBlock.gif 
39InBlock.gif            
40InBlock.gif// send the file stream to the client
41InBlock.gif            
42InBlock.gif            
43InBlock.gifResponse.WriteFile(file.FullName);
44InBlock.gif            
45InBlock.gif// stop the execution of this page
46InBlock.gif            
47InBlock.gifResponse.End();
48InBlock.gif
49InBlock.gif 
50InBlock.gif            
51InBlock.gif
52ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/xiaohua1229/archive/2006/08/17/479564.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值