ASP Upload 使用说明

王朝asp·作者佚名  2006-11-24
宽屏版  字体: |||超大  

使用ASP实现文件上载到WEB服务器

ASPupload 2.0版,相关源文件如下(uploadTest.htm):

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<title>文件上载客户端</title>

</head>

<body>

<form method="POST" action="uploadTest1.asp" enctype="multipart/form-data"

name="UPloadForm">

<p><input type="file" Name="File1"></p>

<p><input type="submit" value="Submit" name="Upload"></p>

</form>

</body>

</html>

其中客户端文件要注意两点:

* 文件上载提交表单(Form)的enctype必须指定为“multipart/form-data”

* 语句<input type="file" Name="File1">表示上载文件域,用户可以在该域中输入或选定文件。

服务器端源文件如下(uploadTest1.asp):

<%response.buffer=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.BinaryWrite rs("Content")

rs.close

rs1.close

conn.close

Response.End

%>

其中,Web Server向客户端发送Content-Type="application/msword",使客户端认为这是Word文档,然后服务器从数据库中读取文件内容(为简单起见,假定是数据库中最后一条记录),然后以二进制流的方式向客户端发送(调用ASP内置对象Response的BinaryWrite方法)。当客户端接收到这些内容后便自动启动Word OLE服务,使Word控件嵌在浏览器IE中将收到的内容格式化显示。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有