加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 智能语音交互、行业智能、AI应用、云计算、5G!
当前位置: 首页 > 站长学院 > Asp教程 > 正文

引用ASP Upload长传文件的方法

发布时间:2023-05-04 13:30:06 所属栏目:Asp教程 来源:
导读:ASPupload 2.0版,相关源文件如下(uploadTest.htm):

<html>

<head>

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

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

</head>

<bo
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中将收到的内容格式化显示。
 
 

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章