欢迎光临
我们一直在努力

ASP和C#隐藏文件下载路径的方法-ASP教程,ASP技巧

建站超值云服务器,限时71元/月

 

关于下载路径的隐藏都是通过传递一个数据库中的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
%>

 

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » ASP和C#隐藏文件下载路径的方法-ASP教程,ASP技巧
分享到: 更多 (0)