惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
aimingoo的专栏
aimingoo的专栏
F
Fortinet All Blogs
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
MongoDB | Blog
MongoDB | Blog
月光博客
月光博客
The Cloudflare Blog
量子位
T
Tailwind CSS Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog
MyScale Blog
MyScale Blog
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
G
Google Developers Blog
D
DataBreaches.Net
V
Visual Studio Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
U
Unit 42
博客园 - 聂微东
有赞技术团队
有赞技术团队
A
About on SuperTechFans

博客园 - 9527的晃悠人生

(转)使用Visual Studio.net调试javascript最方便的方法 jsoho.com介绍 测试 学习计划 简单权限验证的方法 用log4net快速构建asp.net 异常日志 查找某些记录中重复的记录 竞猜系统设计文档 足球联赛竞猜管理 遍厉目录下文件和子目录 用C#实现将html文件转换为chm文件 调试断点的条件设置 Asp.net 中 页面无刷新的做法 内容服务中的大文本处理。 关于Asp的性能优化 Insert 一个字段类型是自动增量的,又要写入变量 第三方的数据控件、分页控件 关于网络文学站的相关事宜 利用PD进行数据库建模
自定义下载文件名
9527的晃悠人生 · 2004-06-16 · via 博客园 - 9527的晃悠人生

无知者无畏,今天遇到一个问题,需要自定义下载文件名,以前在asp中写过,马上把原来代码翻出来了
<%
Dim Stream
Dim Contents
Dim FileName
Dim FileExt
Const adTypeBinary = 1
FileName = Request.QueryString("FileName")
'response.write FileName& request("filetitle")
'response.end
if FileName = "" Then
    Response.Write "无效文件名."
    Response.End
End if
' 下面是不希望下载的文件
FileExt = Mid(FileName, InStrRev(FileName, ".") + 1)
Select Case UCase(FileExt)
    Case "ASP", "ASA", "ASPX", "ASAX", "MDB"
        Response.Write "受保护文件,不能下载."
        Response.End
End Select
' 下载这个文件
'Response.Clear
Response.ContentType = "application/octet-stream"
Response.AddHeader "content-disposition", "attachment; filename=" & request("filetitle")
Set Stream = server.CreateObject("ADODB.Stream")
Stream.Type = adTypeBinary
Stream.Open
Stream.LoadFromFile Server.MapPath(FileName)
While Not Stream.EOS
    Response.BinaryWrite Stream.Read(1024 * 64)
Wend
Stream.Close
Set Stream = Nothing
Response.Flush
Response.End
%>


这样可以防止别人看到文件下载的物理路径,同时也可部分的实现安全下载,在c#里也改了一下
/// <summary>
/// 下载文件
/// </summary>
/// <param name="userID">用户ID号</param>
/// <param name="fileName">要下载的文件名</param>
/// <returns>成功与否</returns>
public static ReturnInfo DownloadFile(string userID,string fileName)
{
 if(fileName.Length<4)
  return new ReturnInfo(0,new Exception("文件名错误!"));
 System.Web.HttpResponse Response = System.Web.HttpContext.Current.Response;
 System.Web.HttpServerUtility Server = System.Web.HttpContext.Current.Server;
 string filePath = Server.MapPath("UserSpace")+"/"+userID+"/"+fileName+".aspx";
 FileInfo file = new FileInfo(filePath);
 if(!file.Exists)
  return new ReturnInfo(0,new Exception("没有找到要下载的文件!"));
 Response.Clear();
 Response.AddHeader("Content-disposition","attachment;filename="+fileName.Replace(".aspx",""));
 Response.AddHeader("Content-length",file.Length.ToString());
 Response.ContentType = "application/octet-stream";
 Response.WriteFile(file.FullName);
 Response.End();

 return new ReturnInfo(true);
}