翱翔.Net

释放.Net的力量
posts - 74, comments - 1116, trackbacks - 4, articles - 17
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理

使用数据流下载文件

Posted on 2004-09-17 10:07 Hover 阅读(2142) 评论(2)  编辑 收藏 网摘 所属分类: WebFormThePhile

正常web服务器上的文件如.aspx.cs.txt.html等IIS会解释执行
.cs等默认是不能访问的。要实现下载这件文件要用到数据流!
这样不用理会IIS

代码如下。DownLoad.aspx.cs

 

private void Page_Load(object sender, System.EventArgs e)
        
{
            
// retrieve the path of the file to download, and create
            
// a FileInfo object to read its properties
            string path = Server.MapPath(Request.Params["File"]);
            System.IO.FileInfo file 
= new System.IO.FileInfo(path);

            
// clear the current output content from the buffer
            Response.Clear();
            
// add the header that specifies the default filename for the Download/SaveAs dialog
            Response.AddHeader("Content-Disposition""attachment; filename=" + file.Name);
            
// add the header that specifies the file size, so that the browser
            
// can show the download progress
            Response.AddHeader("Content-Length", file.Length.ToString());
            
// specify that the response is a stream that cannot be read by the
            
// client and must be downloaded
            Response.ContentType = "application/octet-stream";
            
// send the file stream to the client
            Response.WriteFile(file.FullName);
            
// stop the execution of this page
            Response.End();
        }

 

 

Feedback

#1楼    回复  引用  查看    

2004-09-17 13:33 by myrat      
记得前面讨论过,这样的话会把整个文件读到内存中,如果是大文件的话就惨了
还是应该用stream

#2楼    回复  引用    

2005-01-11 18:49 by 丁相匀 [未注册用户]
可以联系QQ43827651
tel:13563970667




标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
Google站内搜索


China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
近千种 9-95 新二手计算图书火热销售中!

相关文章:

相关链接: