程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> ASP編程 >> ASP技巧 >> ASP和C#隱藏文件下載路徑的方法

ASP和C#隱藏文件下載路徑的方法

編輯:ASP技巧

關於下載路徑的隱藏都是通過傳遞一個數據庫中的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
%>

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved