学院首页>网络编程>ASP>ASP: 浏览器中上载文件的方法与实现

ASP: 浏览器中上载文件的方法与实现

作者:未知 来源:网易 添加时间:2006-5-21 10:32:17
---- 一、问题引入 

---- 在现在的管理信息系统中,比较先进的都已采用浏览器/服务器的模型,在这种模型中都要涉及到客户端与服务器端的信息交互问题,从服务器端到客户端的信息传递技术已经比较成熟,这里主要讨论从客户端到服务器端的文件上载问题,基于Microsoft的IE4.0、IIS4.0、ASP(Active Server Page)和标准HTML语言。 

---- 二、实现方法 

---- 在ASP页面中,利用HTML中的Form元素来实现。 

---- 在Form元素的语法中,EncType表明提交数据的格式,Method表明提交的方法(Get/Post)。在IE4.0 及以后的版本中都支持"multipart/form-data"这种格式,相应的Method方法必须是Post,表明要上载文件到服务器。当然同时在服务器相应的WEB站点上要把属性设为可写。下面是一个示例: 

< form enctype="multipart/form-data" 
action="http://dev_d/upload/post/cpshost.dll? 
PUBLISH?http://dev_d/upload/UserUploadAction.asp" 
method=post id=form2 name=form2 > 

1. Press the Browse button and 
choose a File to upload from your computer. 
< br >< input type="file" id=file1 name=file1 > 
2. Upload the file. 
< br >< input type=hidden size=80 name="TargetURL" 
value=http://dev_d/upload/post > 
< input type=submit value='Upload' 
id=submit1 name=submit1 > 
< /form > 

---- 三、实现要素 

1 Form的enctype="multipart/form-data"。 

2 Form的action="(UserURL)/cpshost.dll?PUBLISH? 
(UserURL)/UserUploadAction.asp"。 

---- 说明:cpshost.dll是用于文件上载的动态链接文件,其后的PUBLISH参数也为固定,而(UserURL)指的是完整的URL地址,如:http://dev_d/upload。如果PUBLISH后没有参数,上载文件完成后,只是简单返回文件已经上载;如果PUBLISH后跟上完整URL的ASP文件,就可以用ASP来处理文件上载后的其他操作,如修改相应的数据库数据。可以在ASP文件中用Request.Form("Variable")来访问相应参数。对上载文件来说,Variable有四种可能的值:FileName 文件名称(不包括后缀),FileExtention 文件后缀(包括"."),FilePath 上载文件保存的服务器端路径,FileSize 上载文件的字节大小。 

---- 3 Form的Method的方法必须为Post。 

---- 4 Form中必须有一个input元素,而且input的属性type="file"。 

---- 说明:如果要上载多个文件,有多个input元素就可以了,但至少有一个有效文件,否则会出错。 

---- 系统会自动产生一个文本区域和一个"browse..."按钮,可以直接在文本区域内输入文件路径名称,或按"browse..."按钮,从文件对话框中选择一个文件。 

---- 5 Form中必须有一个隐含(即type=hidden)input元素,而且input的属性name="TargetURL",属性 

---- value="(UserURL)",(UserURL)即为上载文件保存位置的URL地址。 

---- 说明:文件保存位置的URL地址属性必须设为可写,否则会返回此URL地址没有写的权限。 

---- 6 Form中必须有一个submit按钮,即input的属性type="submit",此按钮即为上载按钮。或者在其他相关事件中调用此Form的Submit方法。但两种方法实际上本质相同,只不过用方法调用还可以在上载前加上其它处理代码,如数据的有效性检查等。 

---- 四、完整实例 

---- 1 用户上载文件页面UserUpload.asp 

< % response.expires=0 % > 
< HTML > 
< HEAD > 
< META NAME="GENERATOR" 
Content="Microsoft Visual Studio 6.0" > 
< /HEAD > 

< BODY > 

< form enctype="multipart/form-data" 
action="http://dev_d/upload/post/cpshost.dll? 
PUBLISH?http://dev_d/upload/UserUploadAction.asp" 
method=post id=form2 name=form2 > 
< table BORDER=0 CELLSPACING=3 CELLPADDING=3 > 
< tr > 
< td valign=top >< span >1. < /span > 
< td >Press the Browse button and choose a File to upload from your computer. 
< br >< input type="file" id=file1 name=file1 > 
< br >< input type="file" id=file2 name=file2 > 
< /td > 
< tr > 
< TD vAlign=top >< SPAN >2. < /SPAN > 
< TD >Upload the file. 
< br >< input type=hidden size=80 name="TargetURL" value="http://dev_d/upload/post" > 
< input type=submit value='Upload' 
id=submit1 name=submit1 > 
< /td > 
< /table > 
< /form > 
< /BODY > 
< /HTML > 

2 用户上载文件处理页面UserUploadAction.asp 
< % Response.Buffer = TRUE % > 
< % Response.expires=0 % > 

< HTML > 
< BODY > 
< H3 >Upload Status:< BR >< /H3 > 

< span style="color:gray" >< HR > 
< % For I = 1 To Request.Form("FileName").Count 
Response.Write "Uploaded File: < B >" & 
Request.Form("FileName")(I) & 
Request.Form("FileExtention")(I) &"< /B >< BR >" 
Response.Write "Server Path: < B >" & 
Request.Form("FilePath")(I) & "< /B >< BR >" 
Response.Write "Size: < B >" 
& Request.Form("FileSize")(I) 
& " bytes< /B >< br >" 
Next 
FileName = Request.Form("FilePath")(1) & 
Request.Form("FileName")(1) & 
Request.Form("FileExtention")(1) 
% > 
< hr >< br > 
< % if request.form("FilePath").count = 0 then 
Response.Write ("No file was received.") 
Response.End 
else 
Response.Write (filename+" File was received.") 
end if % > 
< /span > 
< /BODY > 
< /HTML > 

 
站内搜索