62,067
社区成员
发帖
与我相关
我的任务
分享
<%@ Page Language="C#" EnableViewState="false" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
//一般情况下,通过id传参数,然后查库,查库省略
String id = Request.Params.Get("id");
//安全性验证省略。。。
//根据id查库得到文件路径省略。。。
//假如查出的文件路径是/upload/20130508/1.bmp,
String file = Request.PhysicalApplicationPath + "/upload/20130508/1.bmp";
file = file.Replace("/", "\\").Replace("\\\\","\\");
Response.Clear();
Response.ContentType = "application/octet-stream";
if (Request.UserAgent.Contains("MSIE") || Request.UserAgent.Contains("msie")) {
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("文件下载后的名称", System.Text.Encoding.UTF8) + System.IO.Path.GetExtension(file));
}
else
{
Response.AddHeader("Content-Disposition", "attachment; filename=" + "文件下载后的名称" + System.IO.Path.GetExtension(file));
}
Response.WriteFile(file);//向客户端输出流。
Response.Flush();
Response.End();
}
</script>
string fileName = Request.QueryString["fileName"];//这样容易被人猜出来,或者改成唯一性标号,后台根据此编号获取对应的文件路径
<a href="download.aspx?fileName=aabbcc.zip">aabbcc.zip</a>