問題描述:
在軟件信息上有壹個下載按鈕:
<a href="down?filename=<%=rs("filename")%>"><img src=images/download.gif border=0></a>
在down上計算下載次數,並下載文件:
<!--#include file="conn"-->
<%
set rs2 = server.CreateObject ("Adodb.recordset")
sql2="update downtimes set downtimes=(downtimes+1) where filename="&request("filename")
rs2.open sql2,conn,1,1
set rs2=nothing
set rs = server.CreateObject ("Adodb.recordset")
sql="select * from upfile where filename="&request("filename")
rs.open sql,conn,1,1
Response.Redirect rs("filepath")
%>
filepath壹般是“XXX.mp3”或“XXX.wma”、“XXX.doc”、“XXX”這樣的名稱,當點擊下載按鈕後,瀏覽器不顯示下載對話框,而是直接在瀏覽器中播放或是顯示這些文檔或圖片。請問有沒有什麽辦法,不讓瀏覽器調用本機播放器播放這些影音文件。當然,右鍵“目標另存為”可以下載,但是這樣就不能計算文件的下載次數了,有兩全其美的辦法麽?
解析:
Function downloadFile(strFile)
strFilename = server.MapPath(strFile)
Response.Buffer = True
Response.Clear
Set s = Server.CreateObject("ADODB.Stream")
s.Open
s.Type = 1
on error resume next
Set fso = Server.CreateObject("Scripting.FileSystemObject")
if not fso.FileExists(strFilename) then
Response.Write("<h1>Error:</h1> 文件不存在<p>")
Response.End
end if
Set f = fso.GetFile(strFilename)
intFilelength = f.size
s.LoadFromFile(strFilename)
if err then
Response.Write("<h1>Error: </h1>" & err.Description & "<p>")
Response.End
end if
Response.AddHeader "Content-Disposition", "attachment; filename=" &f.name
Response.AddHeader "Content-Length", intFilelength
Response.CharSet = "UTF-8"
Response.ContentType = "application/octet-stream"
Response.BinaryWrite s.Read
Response.Flush
s.Close
Set s = Nothing
End Function
看這個函數能幫到妳嗎