以流的方式下载文件,隐藏实际的下载路径

本文介绍了一种通过流的方式下载文件,并隐藏实际下载路径的方法,使用了ASP.NET来实现。代码示例包括获取文件路径、设置HTTP响应头、写入文件内容等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自: http://dev.mjxy.cn/a-Download-the-file-to-stream-download-hide-the-real-path.aspx

以流的方式下载文件,隐藏实际的下载路径
 

01string path = Server.MapPath("~/UploadFiles/" + "a.doc");
02        System.IO.FileInfo file = new System.IO.FileInfo(path);
03  
04        Response.Clear();
05        Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
06  
07        Response.AddHeader("Content-Length", file.Length.ToString());
08        Response.ContentType = "application/octet-stream";
09        Response.WriteFile(file.FullName);
10        Response.End();
11  
12public void down(string cc)
13  {
14   if (cc != "")
15   {
16  
17    string path = System.Web.HttpContext.Current.Server.MapPath(cc);
18    System.IO.FileInfo file = new System.IO.FileInfo(path);
19    if (file.Exists)
20    {
21     System.Web.HttpContext.Current.Response.Clear();
22     System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
23  
24     System.Web.HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
25  
26     System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
27  
28     System.Web.HttpContext.Current.Response.Filter.Close();
29     System.Web.HttpContext.Current.Response.WriteFile(file.FullName);
30     System.Web.HttpContext.Current.Response.End();
31    }
32    else
33    {
34     System.Web.HttpContext.Current.Response.Write("文件不存在");
35     System.Web.HttpContext.Current.Response.End();
36    }
37    }
38  }

转载于:https://www.cnblogs.com/xingquan/archive/2011/07/08/2101032.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值