无组件上传几个实例说明

商帝国咨询头像

阅读 13016 | 分类:文章

首先要include upload_5xSoft.inc  这个家都会吧 我就不说了 呵呵!

下面说几个有用的对象的用法:
建立上传对象:set upload=new upload_5xSoft

得到上传路径:upload.form("filepath")

计算所有上传了的文件的个数:
for each formName in upload.file
count=count+1
next

生成文件对象: set file=upload.file(formName)

得到上传的文件名:file.FileName

得到文件的大小:file.FileSize

*最实质的一个用法:
保存文件:
file.SaveAs Server.mappath(formPath&file.FileName)

“”“”“”“注意”“”“”“”“:服务器必须有formPath这个目录我下面的例子这个目录叫data

这样就可以将文件写入服务器的硬盘上了!

用以上方法可以实现最简单的无组件上传:

===============test.htm=============
<form name="myform" action="myupfile.asp" method="post" enctype="multipart/form-data">
<table width="80%" align="center" border="1">
<tr>
     <td>
     上传到:<input type="text" name="filepath" value="data"> 这个类型最好是hidden
     </td>
</tr>

<tr>
     <td>
     <input type="file" name="file1" value="">
     <td>
</tr>
<tr>
     <td>
     <input type="file" name="file2" value="">
     <td>
</tr>

<tr>
     <td>
     <input type="submit" name="submit" value="上传">
     <td>
</tr>
</table>
</form>

===============myupfile.asp=============

<!--#include file="upload_5xsoft.inc"-->
<%
dim upload,file,formName,formPath,iCount
set upload=new upload_5xSoft ''建立上传对象

if upload.form("filepath")="" then ''得到上传目录
response.write "请选择上传至目录!"
else
formPath=upload.form("filepath")

'-----在目录后加"/"------'
if right(formPath,1)<>"/" then formPath=formPath&"/"

for each formName in upload.file ''列出所有上传了的文件
set file=upload.file(formName) ''生成一个文件对象
if file.FileSize>0 then ''如果 FileSize > 0 说明有文件数据
file.SaveAs Server.mappath(formPath&file.FileName) ''保存文件
response.write formPath&file.FileName&"已上传成功!
"
end if
next

end if
set upload=nothing
set file=nothing
response.end
%>

****再次提醒注意 *******
运行上面的程序 在同级目录下必须有formPath目录既data目录!!!

============================================
=如果有朋友想限制上传大小、类型、个数我有如下提示:=
============================================
大小:file.FileSize可以搞定 如
 if file.filesize>400000 then
       response.write "文件大小超过了限制 400K"
     response.end
 end if
类型:截取扩展名:如
 fileExt=lcase(right(file.filename,4))
 if fileEXT<>".gif" and fileEXT<>".jpg" then
       response.write "文件格式不正确"
     response.end
 end if

 这个一定要设好哦,小心给你上传病毒!嘿嘿!
个数:
其实你的test.htm里只给他一个<input type="file" name="file1" value="">
但在这里也可以:
for each formName in upload.file
count=count+1
next
if count>2 then
response.write "只能上传2个文件"
response.end
end if

留言区(共0条)

暂无留言,快来抢占沙发~
作者声明:非原创
平台声明:文章及评论仅代表作者本人观点,五略商书系信息发布平台,仅提供信息存储空间服务,如有侵权,请联系13521509118。转载请注明作者和出处:转自五略商书m.shangdiguo.com,作者商帝国咨询!