2013-11-04 128 views
3

我有一個網站正在建立ASP經典,我有一些腳本,允許用戶下載文件,但隱藏文件的路徑的麻煩。ASP經典下載文件腳本

當用戶在頁面上時,他們將看到一個鏈接。鏈接編碼是這樣的:

<a href="download.asp?file=FILE-NAME-HERE" target="_blank">Download File</a> 

此鏈接帶他們去哪裏download.asp運行代碼來獲取文件,並提供它。下面是代碼我現在所擁有的:

<% 
const adTypeBinary = 1 
dim strFilePath, strFile 

strFilePath = "/_uploads/private/" 
strFile = Request.QueryString("file") 

if strFile <> "" then 
    'Set the content type to the specific type that you are sending. 
    Response.ContentType = "application/octet-stream" 
    Response.AddHeader "Content-Disposition", "attachment; filename=" & strFile 

    set objStream = Server.CreateObject("ADODB.Stream") 
    objStream.open 
    objStream.type = adTypeBinary 
    objStream.LoadFromFile(strFilePath & strFile) 

    response.binarywrite objStream.Read 

    objStream.close 
    Set objStream = nothing 

end if 
%> 

此代碼我從這個網站(How to download the files using vbscript in classic asp)這兩個問題放在一起,並從http://support.microsoft.com/kb/276488

正在發生的事情,然而,就是在download.asp頁面給我一個「找不到文件」的錯誤,即使該文件在web目錄「/ _uploads/private /」中正確。

的文件類型可以是幾個,包括PDF,XLS,DOCX一個等

有什麼在我的代碼是不允許的文件被發現?

+0

IIS是否具有該文件夾的讀取權限? –

+0

是的,「/ _uploads/private /」文件夾的當前權限值爲755. – user2762748

+1

嘗試使用整個驅動器號+路徑。 –

回答

2

感謝用戶oracle認證的專業人士,在上面的評論。

什麼工作是添加「Server.MapPath」來解決文件的位置。

而不是使用:

objStream.LoadFromFile(strFilePath & strFile) 

我把它改爲:

objStream.LoadFromFile(Server.MapPath(strFilePath & strFile)) 

現在的鏈接觸發文件正確下載。