在_blank>浏览器的地址栏里直接输入一个doc或xls或jpg的文件的url路径,那么该文件会直接显示在_blank>浏览器里。而在很多时候我们希望能直接弹出下载提示框让用户下载,我们该怎么办呢?这里有两种方法: 1、设置你的服务器" target=_blank>服务器的iis,给doc等后缀名做映射 2、在向客户端发送时设置其contenttype
- k9 W8 S: g( V) J* J下面详细说明方法2
5 q( B* Z H `& ~5 K7 p" r9 j D% F<% i5 D+ n, r" ^" ^( B8 y" M& B, U( J Response.Buffer = true* a0 ~# p% [- u" {. v Response.Clear
dim url$ ~ F- I% L7 d Dim fso,fl,flsize* |. ]3 v+ w; y5 \7 R A! F+ }, Z dim Dname3 Y3 ?- h0 v5 C* v" i Dim objStream,ContentType,flName,isre,url1 '*********************************************调用时传入的下载文件名 Dname=trim(request("n"))7 Z8 x4 G$ H R3 j' h* _5 I '******************************************************************& y& @% @: }4 r If Dname<>"" Then) l# ^8 V9 U6 m8 Q- {* W9 Q9 N0 Z '******************************下载文件存放的服务端目录 url=server.MapPath("/")&"\"&Dname* v& {0 {' o+ u2 y4 o '*************************************************** End If
Set fso=Server.CreateObject("Scripting.FileSystemObject") Set fl=fso.getfile(url) flsize=fl.size flName=fl.name; N/ H7 U' x) b" c0 `; g Set fl=Nothing Set fso=Nothing! A G0 z5 [; n# K" W# x0 j& s0 X %> <%) m0 c8 `9 l% M. t, I/ s. z Set objStream = Server.CreateObject("ADODB.Stream"); G/ |* ~" t9 r" ~. c$ C0 U objStream.Open objStream.Type = 1, e6 U% w E5 m1 B$ w9 b$ D; {6 s objStream.LoadFromFile url
9 P) N$ Q# \* V, k8 KSelect Case lcase(Right(flName, 4))' ?2 @- U* ^. w8 Q Case ".asf" ContentType = "video/x-ms-asf" Case ".avi" ContentType = "video/avi"1 o5 R) b0 N/ `4 g Case ".doc" ContentType = "application/msword" Case ".zip" ContentType = "application/zip", p7 N8 G3 ~: q B# T+ @; \ Case ".xls" ContentType = "application/vnd.ms-excel"* \* t! J! w, I0 ~) l Case ".gif"! q; z4 t; }/ K. w: y h ContentType = "image/gif" X8 t' H. m. k( a2 ]2 ~ Case ".jpg", "jpeg"" Z. F7 ~% s# N J1 v ContentType = "image/jpeg" M/ F( p# h& Z* q# b( D { Case ".wav"+ q' g$ ^0 @. R$ E ContentType = "audio/wav": i9 F4 a" b& {# u Case ".mp3" ContentType = "audio/mpeg3"# e; P9 |, r2 ^$ M1 T! L" | Case ".mpg", "mpeg" ContentType = "video/mpeg" Case ".rtf" ContentType = "application/rtf"# ]) Y( j9 M+ M4 m! H Case ".htm", "html" ContentType = "text/html" Case ".txt"( Q% _$ L" R( Y2 w* Y: _ ContentType = "text/plain") |% n' {- ~5 f Case Else4 R/ B6 I# S6 Y: S' G; M/ O ContentType = "application/octet-stream": y* p! E! f' H. M: H End Select
Response.AddHeader "Content-Disposition", "attachment; filename=" & flName$ e& W5 r6 q0 U; V+ L% E- l3 v Response.AddHeader "Content-Length", flsize
2 S# U$ D8 _. \6 q/ f- UResponse.Charset = "UTF-8" Response.ContentType = ContentType
Response.BinaryWrite objStream.Read. @( D* ]7 K# g5 v, J Response.Flush! z& V9 {+ `" s& @% S response.Clear() objStream.Close Set objStream = Nothing
%>
将下面的东西存成download.asp然后你就可以用<a herf="download!http://www.knowsky.com/download.asp?n=file.doc">download!</a>来下载同一目录下的file.doc了!
但是这里有个问题就是直接将file.doc路径写在url里是不安全的,所以解决方案应该是将file.doc的路径存到数据库" target=_blank>数据库里,同过查找数据库" target=_blank>数据库后得到路径
在这个程序的最前面如果加上一个判断:
if instr(Request.ServerVariables("HTTP_REFERER"),"http://你的_blank>域名")=0 then1 B0 H; |* y; L Response.End" U' n6 ^5 ~, m* Z' ~1 K end if
就能够很好的防止别人的盗链了
| 欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) | Powered by Discuz! X2.5 |