关于下载路径的隐藏都是通过传递一个数据库中的ID,然后在另外一张页面通过传递的ID在数据库中读出路径。
c#中实现的方法(只需要.ASPX.CS的代码就可以了)
string sel_sql="select FileVisualPath from TabMailAttachFiles where FileID="+Request.QueryString["destFileName"].ToString();

// string destFileName = Request.QueryString["destFileName"]!=null?Request.QueryString["destFileName"]:"";
string destFileName=ROA.Components.fune_commerce.Execad(sel_sql).Tables[0].Rows[0][0].ToString();
destFileName = Server.MapPath(".")+destFileName;
destFileName = Server.UrlDecode(destFileName);
if(File.Exists(destFileName))

{
FileInfo fi = new FileInfo(destFileName);
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
//Response.AppendHeader("Content-Disposition","attachment;filename=" +HttpUtility.UrlEncode(Path.GetFileName(destFileName),System.Text.Encoding.Default));
Response.AppendHeader("Content-Disposition","attachment;filename=" +HttpUtility.UrlEncode(Path.GetFileName(destFileName),System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length",fi.Length.ToString());
Response.ContentType="application/octet-stream";
Response.WriteFile(destFileName);
Response.Flush();
Response.End();
}
else

{
Response.Write("<script langauge=javascript>alert('文件不存在!');history.go(-1);</script>");
Response.End();
}
c#中实现的方法(只需要.ASPX.CS的代码就可以了)
string sel_sql="select FileVisualPath from TabMailAttachFiles where FileID="+Request.QueryString["destFileName"].ToString();
// string destFileName = Request.QueryString["destFileName"]!=null?Request.QueryString["destFileName"]:"";
string destFileName=ROA.Components.fune_commerce.Execad(sel_sql).Tables[0].Rows[0][0].ToString();
destFileName = Server.MapPath(".")+destFileName;
destFileName = Server.UrlDecode(destFileName);
if(File.Exists(destFileName))
{
FileInfo fi = new FileInfo(destFileName);
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
//Response.AppendHeader("Content-Disposition","attachment;filename=" +HttpUtility.UrlEncode(Path.GetFileName(destFileName),System.Text.Encoding.Default));
Response.AppendHeader("Content-Disposition","attachment;filename=" +HttpUtility.UrlEncode(Path.GetFileName(destFileName),System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length",fi.Length.ToString());
Response.ContentType="application/octet-stream";
Response.WriteFile(destFileName);
Response.Flush();
Response.End();
}
else
{
Response.Write("<script langauge=javascript>alert('文件不存在!');history.go(-1);</script>");
Response.End();
}
本文介绍了一种使用C#实现的文件下载路径隐藏方法。该方法通过传递数据库ID来间接获取文件路径,增强了文件的安全性。具体步骤包括从数据库读取文件路径、检查文件是否存在、设置响应头并发送文件内容。

1308

被折叠的 条评论
为什么被折叠?



