把存储在SQL7的image字段的文件下载到客户端的ASP源代码
文 件 名:download.asp
使用方法:download.asp?fid=xxx
说 明:把SQL7的image字段存储的文件下载到客户端
数据库结构:[表名]tabimage {fid int not null;filename varchar(100) not null;filecontent image not null}
fid:文件id [PK];filename:文件名;filecontent:文件二进制内容
1
2Response.Buffer=True
3varfileid = Request("fid")
4If varfileid="" Then
5Response.write "没有指定下载文件ID。"
6Response.End
7End If
8
9OpenDB conn
10SQL = "SELECT filename,filecontent FROM tabimage WHERE fid=" & varfileid
11Set rs = conn.Execute(SQL)
12If Not rs.Eof Then
13varfilename = rs("filename")
14varfilesize=rs("filecontent").ActualSize
15varcontent = rs("filecontent").GetChunk(varfilesize)
16Response.ContentType = "*/*"
17Response.AddHeader "Content-Length",varfilesize
18Response.AddHeader "Content-Disposition", "attachment;filename=""" & varfilename & """"
19Response.binarywrite varcontent
20End If
21rs.Close
22Set rs = Nothing
23conn.Close
24Set conn = Nothing
25Response.End
26
27'连接数据库通用过程
28Sub OpenDB (ByRef conn)
29Set conn = Server.CreateObject("ADODB.Connection")
30conn.provider="sqloledb"
31conn.ConnectionString = "driver={SQL Server};server=xxx.xxx.xxx.xxx;uid=myusername;pwd=mypassword;database=mydatabase"
32conn.Open
33End Sub