熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

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

2013-11-13 10:20:44  來源: .NET編程 
    關於下載路徑的隱藏都是通過傳遞一個數據庫中的ID然後在另外一張頁面通過傳遞的ID在數據庫中讀出路徑
c#中實現的方法(只需要ASPXCS的代碼就可以了)
 string sel_sql=select FileVisualPath from TabMailAttachFiles where FileID=+RequestQueryString[destFileName]ToString();

  //   string destFileName = RequestQueryString[destFileName]!=null?RequestQueryString[destFileName]:;
   string destFileName=ROAComponentsfune_commerceExecad(sel_sql)Tables[]Rows[][]ToString();
   destFileName = ServerMapPath()+destFileName;
   destFileName = ServerUrlDecode(destFileName);
   if(FileExists(destFileName))
   {
    FileInfo fi = new FileInfo(destFileName);
    ResponseClear();
    ResponseClearHeaders();
    ResponseBuffer = false;
   
    //ResponseAppendHeader(ContentDispositionattachment;filename= +HttpUtilityUrlEncode(PathGetFileName(destFileName)SystemTextEncodingDefault));
    ResponseAppendHeader(ContentDispositionattachment;filename= +HttpUtilityUrlEncode(PathGetFileName(destFileName)SystemTextEncodingUTF));
    ResponseAppendHeader(ContentLengthfiLengthToString());
    ResponseContentType=application/octetstream;
    ResponseWriteFile(destFileName);
    ResponseFlush();
    ResponseEnd();
   }
   else
   {
    ResponseWrite(<script langauge=javascript>alert(文件不存在!);historygo();</script>);
    ResponseEnd();
   }


    在ASP中的實現方法
 <!#include file=connasp >
<%
ResponseBuffer = true
ResponseClear
dim url
Dim fsoflflsize
dim Dname
Dim objStreamContentTypeflNameisreurl
*********************************************調用時傳入的下載文件名
m_ID=trim(requestQueryString(ID))
set rs=ServerCreateObject(adodbrecordset)
sql=select * from dataview where xsf=true and id=&m_ID
rsopen sqlconn
if not rseof then
    Dname=rs(path)
else
    ResponseWrite(no found)
    Responseend
end if
rsclose:set rs=nothing
******************************************************************
If Dname<> Then
******************************下載文件存放的服務端目錄
url=ServerMapPath(Dname)
else
    ResponseWrite(no found)
    Responseend 
End If
Set fso=ServerCreateObject(ScriptingFileSystemObject)
Set fl=fsogetfile(url)
flsize=flsize
flName=flname
Set fl=Nothing
Set fso=Nothing
Set objStream = ServerCreateObject(ADODBStream)
objStreamOpen
objStreamType =
objStreamLoadFromFile url
Select Case lcase(Right(flName ))
Case asf
ContentType = video/xmsasf
Case avi
ContentType = video/avi
Case doc
ContentType = application/msword
Case zip
ContentType = application/zip
Case xls
ContentType = application/vndmsexcel
Case gif
ContentType = image/gif
Case jpg jpeg
ContentType = image/jpeg
Case bmp
ContentType = image/bmp
Case ppt
ContentType = application/vndmspowerpoint
Case mdb
ContentType = application/xmsaccess
Case wav
ContentType = audio/wav
Case mp
ContentType = audio/mpeg
Case mpg mpeg
ContentType = video/mpeg
Case rtf
ContentType = application/rtf
Case html
ContentType = text/html
Case txt
ContentType = text/plain
Case Else
ContentType = application/octetstream
End Select
ResponseAddHeader ContentDisposition attachment; filename= & flName
ResponseAddHeader ContentLength flsize
ResponseCharset = UTF
ResponseContentType = ContentType
ResponseBinaryWrite objStreamRead
ResponseFlush
responseClear()
objStreamClose
Set objStream = Nothing
%>
From:http://tw.wingwit.com/Article/program/net/201311/13313.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.