艺术迷网
艺术迷 >> 网页设计 >> 其它教程 >> ASPupload组件使用帮助
2006-9-14   作者:佚名   来源:艺术迷收集(fansart.com)   点击:1810
页面功能: 【评论】有错就点】 【字体: 】 【打印】 【关闭【收藏本页】收藏到365Key
 
使用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中将收到的内容格式化显示。

声明:本站刊载此文不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。如有疑问请与站长联系。
网友评论
目前没有评论!
发表评论
您的姓名: 匿名发送
电子邮件:
评论内容:
不能超过100个字符



所有留言只代表网友个人观点,不代表本站观点。
请各位遵纪守法并注意语言文明。
新闻搜索
关 键 词:
搜索范围:
全站精华
图文精彩