asp 下载功能实现及调用
ASP文件下载主要是头部的输出
'输出文件类型
'"application/force-download"这个是通用的,也可以根据文件类型使用其它如"image/jpeg"之类的
ResponseAddHeader "Content-Type","application/force-download"
'输出文件名,保存文件时会在保存对话框下显示这个文件名。而不是你的asp文件名
ResponseAddHeader "Content-Disposition","attachment; filename="&fName&";"
ASP存取数据库中的二进制数据使用AppendChunk和getChunk,
输出文件内容使用
ResponseBinaryWrite 文件内容(二进制格式)
不懂可以再问
调用
responseWrite("<a href=downaspfilename="&UpLoadPath&ls_array(i+1)&">"&ls_array
(i)&"</td></tr>")
downasp文件内容如下:
<%
Const FilePath = "UploadFile/" '文件存放路径
From_url = Cstr(RequestServerVariables("HTTP_REFERER"))
Serv_url = Cstr(RequestServerVariables("SERVER_NAME"))
Function GetFileName(longname)'/folder1/folder2/fileasp=>fileasp
while instr(longname,"/")
longname = right(longname,len(longname)-1)
wend
GetFileName = longname
End Function
Dim Stream
Dim Contents
Dim FileName
Dim TrueFileName
Dim FileExt
Const adTypeBinary = 1
FileName = RequestQueryString("FileName")
if FileName = "" Then
ResponseWrite "无效文件名!"
ResponseEnd
End if
FileExt = Mid(FileName, InStrRev(FileName, "") + 1)
ResponseClear
if lcase(right(FileName,3))="gif" or lcase(right(FileName,3))="jpg" or lcase(right(FileName,3))="png" then
ResponseContentType = "image/" '对图像文件不出现下载对话框
else
ResponseContentType = "application/ms-download"
end if
ResponseAddHeader "content-disposition", "attachment; filename=" & GetFileName(RequestQueryString("FileName"))
Set Stream = serverCreateObject("ADODBStream")
StreamType = adTypeBinary
StreamOpen
TrueFileName= FilePath &FileName
ResponseWrite TrueFileName
ResponseEnd
StreamLoadFromFile ServerMapPath(TrueFileName)
While Not StreamEOS
ResponseBinaryWrite StreamRead(1024 64)
Wend
StreamClose
Set Stream = Nothing
ResponseFlush
ResponseEnd
%>
首先你要把下载链接的地址存储到数据库里,或者你写个实体类,从文件系统的指定文件夹里遍历读取地址也可以。
然后从数据库或者实体类返回的数据中取得地址,绑定在GirdView或者其他DataControl的某一Hyperlink列上。
判断登录状态很简单,用个SESSION变量啦,相信你已经搞定了。
关键是如何提供一个登录后才存在的下载文件。
可以利用对IIS的远程管理,针对已经登录准备下载的SESSION临时创建一个虚拟目录,这个虚拟目录映射了下载文件的物理路径,然后在网页中发布这个虚拟目录或重定向到这个虚拟目录,这样登录用户就可以下载这个文件了。当登录用户注销或登录超时的时候,马上删除这个虚拟目录。这样就只有登录用户才能下载这个文件了。没登录的用户想通过直接链接这个虚拟目录来下载是做不到的,因为没登录时这个虚拟目录不存在! 当然要设法让虚拟目录的名称古怪并且每次都不重样(如用MD5变换SESSIONID)。
但如何在ASPNET中操纵IIS创建、删除虚拟目录呢?这要用到SystemDirectoryServices名字空间。以下是创建/删除虚拟目录的示例:
using SystemDirectoryServices;
void CreateVDir()
{
// metabasePath is of the form "IIS://<servername>/<service>/<siteID>/Root[/<vdir>]"
// for example "IIS://localhost/W3SVC/1/Root"
// vDirName is of the form "<name>", for example, "MyNewVDir"
// physicalPath is of the form "<drive>:\<path>", for example, "C:\Inetpub\Wwwroot"
ConsoleWriteLine("\nCreating virtual directory {0}/{1}, mapping the Root application to {2}:",
metabasePath, vDirName, physicalPath);
string metabasePath=@"IIS://localhost/W3SVC/1/Root";
string vDirName="1234"; // 假设要创建的虚拟目录是/1234
string physicalPath=@"d:\rarfiles"; // 假设d:\rarfiles是服务器上放置下载文件的物理路径
try
{
DirectoryEntry site = new DirectoryEntry(metabasePath);
string className = siteSchemaClassNameToString();
if ((classNameEndsWith("Server")) || (classNameEndsWith("VirtualDir")))
{
DirectoryEntries vdirs = siteChildren;
DirectoryEntry newVDir = vdirsAdd(vDirName, (classNameReplace("Service", "VirtualDir")));
newVDirProperties["Path"][0] = physicalPath;
newVDirProperties["AccessScript"][0] = true;
newVDirProperties["AppFriendlyName"][0] = vDirName;
newVDirProperties["AppIsolated"][0] = "1";
newVDirProperties["AppRoot"][0] = "/LM" + metabasePathSubstring(metabasePathIndexOf("/", ("IIS://"Length)));
newVDirCommitChanges();
}
else
{
// 创建没成功
}
}
catch (Exception ex)
{
// 创建没成功
}
}
void DeleteVDir()
{
string metabasePath="IIS://localhost/W3SVC/1/Root/1234"; // 假设要删除的虚拟目录是/1234
try
{
DirectoryEntry tree = new DirectoryEntry(metabasePath);
treeDeleteTree();
treeCommitChanges();
}
catch (DirectoryNotFoundException ex)
{
// 虚拟目录本来就不存在
}
catch (Exception ex)
{
// 删除失败
}
}
这只是粗略的方法与思路,具体实现时还需要大量的细致工作。
直接发送下载的代码,
Set objStream = ServerCreateObject("ADODBStream")
objStreamOpen
objStreamType = 1
objStreamLoadFromFile filename
Select Case lcase(Right(filename, 4))
Case "asf"
ContentType = "video/x-ms-asf"
Case "avi"
ContentType = "video/avi"
Case "doc"
ContentType = "application/msword"
Case "xls"
ContentType = "application/vndms-excel"
Case "gif"
ContentType = "image/gif"
Case "jpg", "jpeg"
ContentType = "image/jpeg"
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
ResponseAddHeader "Content-Disposition", "attachment; filename=" & shortName
ResponseAddHeader "Content-Length", fileSize
ResponseCharset = "UTF-8"
ResponseContentType = ContentType
ResponseBinaryWrite objStreamRead
ResponseFlush
responseClear()
objStreamClose
Set objStream = Nothing
你把要下载的文件名传到下载页面,用request("fileNameField")获取文件名
下面这地方改一下
iConcStr = "Provider=MicrosoftJetOLEDB40;Persist Security Info=False" & _
";Data Source=" & servermappath(request("fileNameField"))
点击下载的地方用<a href='下载页面路径fileNameField=要下载的文件名'>下载文件</a>
这个
嗯,这个其实就是一个超链接连过去就行了。要知道远程文件的url就可以。
比如说:<a href="http://wwwrarlabcom/rar/wrar393exe">下载</a>
或是用js打开远程文件的url。
一般来说迅雷都能够捕捉到的,但并不能100%确保(谁都不能确保)
缺点:远程文件如果设置了防盗链,就没用了
如果你说的“远程服务器”是内网的,那没办法做到的
downloadasp
<%
'Code By oday
url =Trim(RequestQueryString("url")) '注意URL路径上的文件不能是被IIS解析的,如txt就不行,要用的话自己改个后缀
fname=Trim(RequestQueryString("fname"))
if url <> "" then 'and fname<>"" then
Set xPost = CreateObject("MicrosoftXMLHTTP")
xPostOpen "GET",url,False
xPostSend()
Set sGet = CreateObject("ADODBStream")
sGetMode = 3
sGetType = 1
sGetOpen()
sGetWrite(xPostresponseBody)
sGetSaveToFile ServerMapPath("")&"/"&fname,2
set sGet = nothing
set sPOST = nothing
responseWrite("下载成功!<br>")
end if
%>
testasp
<script>
locationhref="download1aspurl="+escape("http://58211102206/hi/流行音乐/青花瓷mp3")+"&fname=demomp3"
</script>
现在 这个可以运行了!!原来那个 ajax 有点问题!
0条评论