首页 > 编程 > ASP > 正文

ASP和C#隐藏文件下载路径的方法

2024-05-04 11:06:39
字体:
来源:转载
供稿:网友
中国最大的web开发资源网站及技术社区,

  关于下载路径的隐藏都是通过传递一个数据库中的id,然后在另外一张页面通过传递的id在数据库中读出路径。

  1、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();
   }

  2、在asp中的实现方法

<!--#include file="conn.asp" -->
<%
response.buffer = true
response.clear
dim url
dim fso,fl,flsize
dim dname
dim objstream,contenttype,flname,isre,url1
'*********************************************调用时传入的下载文件名
m_id=trim(request.querystring("id"))
set rs=server.createobject("adodb.recordset")
sql="select * from dataview where xsf=true and id="&m_id
rs.open sql,conn,1,1
if not rs.eof then
    dname=rs("path")
else
    response.write("no found")
    response.end
end if
rs.close:set rs=nothing
'*********************************************
if dname<>"" then
'******************************下载文件存放的服务端目录
url=server.mappath(dname)
else
    response.write("no found")
    response.end 
end if
set fso=server.createobject("scripting.filesystemobject")
set fl=fso.getfile(url)
flsize=fl.size
flname=fl.name
set fl=nothing
set fso=nothing
set objstream = server.createobject("adodb.stream")
objstream.open
objstream.type = 1
objstream.loadfromfile url
select case lcase(right(flname, 4))
case ".asf"
contenttype = "video/x-ms-asf"
case ".avi"
contenttype = "video/avi"
case ".doc"
contenttype = "application/msword"
case ".zip"
contenttype = "application/zip"
case ".xls"
contenttype = "application/vnd.ms-excel"
case ".gif"
contenttype = "image/gif"
case ".jpg", "jpeg"
contenttype = "image/jpeg"
case ".bmp"
contenttype = "image/bmp"
case ".ppt"
contenttype = "application/vnd.ms-powerpoint"
case ".mdb"
contenttype = "application/x-msaccess"
case ".wav"
contenttype = "audio/wav"
case ".mp3"
contenttype = "audio/mpeg3"
case ".mpg", "mpeg"
contenttype = "video/mpeg"
case ".rtf"
contenttype = "application/rtf"
case ".htm", "html"
contenttype = "text/html"
case ".txt"
contenttype = "text/plain"
case else
contenttype = "application/octet-stream"
end select
response.addheader "content-disposition", "attachment; filename=" & flname
response.addheader "content-length", flsize
response.charset = "utf-8"
response.contenttype = contenttype
response.binarywrite objstream.read
response.flush
response.clear()
objstream.close
set objstream = nothing
%>

出处:『 漂失在蓝天下的云 』 blog

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表