er=true%>
<html>
<%Set Upload=Server.createobject("Persits.Upload.1") ''创建文件上载组件
Count=Upload.Save("e:\aspupload") ''将客户端文件保存到WEB服务器端的本地硬盘上%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Upload Test</title>
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
</head>
<body>
<p>上载了<%=Count%>个文件</p>
<p>File has been uploaded.</p>
</body>
</html>
其中,脚本Set Upload=Server.createobject("Persits.Upload.1")创建了文件上载组件,该组件通过调用Save方法将浏览器端的文件内容保存到指定路径。
将文件存在数据库中
将文件保存在数据库中主要用了ASPUpLoad组件中文件对象的ToDatabase方法。源文件如下:
客户端源文件(uploadToDB.htm):
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>文件上载客户端</title>
</head>
<body>
<form method="POST" action="UploadToDB.asp" enctype="multipart/form-data"
name="FormUpload">
<p><input type="file" name="FileUpload"> </p>
<p><input type="submit" value="上载" name="B1"></p>
</form>
<p><a href="readFile.asp">读取数据库中文件</a></p>
</body>
</html>
服务器端源文件(uploadToDB.asp):
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>文件上载到数据库</title>
</head>
<body>
<%Set Upload=Server.createobject("Persits.Upload.1")
Count=Upload.Save("e:\aspupload")
on error resume next
set FileObj=Upload.Files("FileUpload")
SQL="insert into upLoadFile (Title,FileName,Content) values (''"&FileObj.Path&"'',''"&FileObj.Path&"'',?)"
response.write SQL
FileObj.ToDatabase "DSN=FileUpload;UID=sa;",SQL
if Err<>0 then
Response.write "Error Saving the file:"&Err.Description
else
FileObj.delete
response.write "Success!"
end if
%>
</body>
</html>
从数据库中读取文件内容并发送给客户端浏览器
从数据库中读取内容在发送给浏览器之前,首先必须让浏览器知道内容的数据类型,这通过向客户端发送ContentType描述实现。为简单起见,这里假设发送的内容是Word文档,并且显示最新插入的记录。源文件如下:
客户端源文件为uploadToDB.htm(同上一部分的客户端文件)。
服务器端源文件(readFile.asp):
<%Response.Expires = 0
response.buffer=true%>
<%response.clear
Response.ContentType = "application/msword"
set conn=server.createobject("adodb.connection")
conn.open "DSN=FileUpload;UID=sa;"
set rs1=conn.execute("select maxid=max(id) from uploadFile")
SQL="select * from uploadFile where id="&rs1("maxid")
set rs=conn.execute(SQL)
Response.B