当前位置: 移动技术网 > IT编程>开发语言>.net > asp将本地的文件上传到服务器

asp将本地的文件上传到服务器

2017年12月12日  | 移动技术网IT编程  | 我要评论

蔚普,架空历史之大唐风云,潮吹美人鱼

今天我们讲解如何利用asp的上传功能将本地的文件上传到服务器上。
最简系统包括下面三个文件:

upload.htm                         --上传口文件,选择本地文件
uploadimg.asp                  --上传程序控制文件
upload_5xsoft.inc            --无组件上传类,此文件初学者不用学习,只要会用就可以了

upload.htm内容————上传口文件,选择本地文件

<html>
<head>
</head>

<body>
<table width="80%" border="0" align="center">
<form name="form1" method="post" action="uploadimg.asp" enctype="multipart/form-data">
<tr>
  <td align="center"><input name="upfile" type="file" id="upfile"></td>
 </tr>
   <tr>
  <td align="center"><input type="submit" name="submit" value="上传图片"></td>
 </tr>
 </form>
</table>
</body>
</html>

uploadimg.asp内容————上传程序控制文件

<!--#include file="upload_5xsoft.inc"-->
<%
dim upload,file,filepath
filepath="upload/"
set upload=new upload_5xsoft ''建立上传对象
for each formname in upload.file ''列出所有上传了的文件
 set file=upload.file(formname) ''生成一个文件对象
 if file.filesize>0 then     ''如果 filesize > 0 说明有文件数据
 fname = file.filename
 file.saveas server.mappath(filepath&fname)  ''保存文件
 end if
set file=nothing
next
set upload=nothing ''删除此对象

upload_5xsoft.inc内容————此文件内容不属于本演练程序内容,本演练应用此类的方法

<script runat=server language=vbscript>
dim oupfilestream

class upload_5xsoft
 
dim form,file,version
 
private sub class_initialize 
dim requestbindate,sstart,bcrlf,sinfo,iinfostart,iinfoend,tstream,istart,ofileinfo
dim ifilesize,sfilepath,sfiletype,sformvalue,sfilename
dim ifindstart,ifindend
dim iformstart,iformend,sformname
version="无组件上传类 version 0.93"
set form=server.createobject("scripting.dictionary")
set file=server.createobject("scripting.dictionary")
if request.totalbytes<1 then exit sub
set tstream = server.createobject("adodb.stream")
set oupfilestream = server.createobject("adodb.stream")
oupfilestream.type = 1
oupfilestream.mode =3
oupfilestream.open
oupfilestream.write request.binaryread(request.totalbytes)
response.write "<font size=""2"">页面执行时间:"&formatnumber((timer() -time1)*1000,3)&"毫秒</font><br>"
oupfilestream.position=0
requestbindate =oupfilestream.read 
iformstart = 1
iformend = lenb(requestbindate)
bcrlf = chrb(13) & chrb(10)
sstart = midb(requestbindate,1, instrb(iformstart,requestbindate,bcrlf)-1)
istart = lenb (sstart)
iformstart=iformstart+istart+1
while (iformstart + 10) < iformend 
 iinfoend = instrb(iformstart,requestbindate,bcrlf & bcrlf)+3
 tstream.type = 1
 tstream.mode =3
 tstream.open
 oupfilestream.position = iformstart
 oupfilestream.copyto tstream,iinfoend-iformstart
 tstream.position = 0
 tstream.type = 2
 tstream.charset ="gb2312"
 sinfo = tstream.readtext   
 '取得表单项目名称
 iformstart = instrb(iinfoend,requestbindate,sstart)
 ifindstart = instr(22,sinfo,"name=""",1)+6
 ifindend = instr(ifindstart,sinfo,"""",1)
 sformname = mid (sinfo,ifindstart,ifindend-ifindstart)
 '如果是文件
 if instr (45,sinfo,"filename=""",1) > 0 then
 set ofileinfo=new fileinfo
 '取得文件名
 ifindstart = instr(ifindend,sinfo,"filename=""",1)+10
 ifindend = instr(ifindstart,sinfo,"""",1)
 sfilename = mid (sinfo,ifindstart,ifindend-ifindstart)
 ofileinfo.filename=getfilename(sfilename)
 ofileinfo.filepath=getfilepath(sfilename)
 '取得文件类型
 ifindstart = instr(ifindend,sinfo,"content-type: ",1)+14
 ifindend = instr(ifindstart,sinfo,vbcr)
 ofileinfo.filetype =mid (sinfo,ifindstart,ifindend-ifindstart)
 ofileinfo.filestart =iinfoend
 ofileinfo.filesize = iformstart -iinfoend -3
 ofileinfo.formname=sformname
 file.add sformname,ofileinfo
 else
 '如果是表单项目
 tstream.close
 tstream.type =1
 tstream.mode =3
 tstream.open
 oupfilestream.position = iinfoend 
 oupfilestream.copyto tstream,iformstart-iinfoend-3
 tstream.position = 0
 tstream.type = 2
 tstream.charset ="gb2312"
 sformvalue = tstream.readtext 
 form.add sformname,sformvalue
 end if
 tstream.close
 iformstart=iformstart+istart+1
 wend
requestbindate=""
set tstream =nothing
end sub

private sub class_terminate 
if not request.totalbytes<1 then
 form.removeall
 file.removeall
 set form=nothing
 set file=nothing
 oupfilestream.close
 set oupfilestream =nothing
 end if
end sub
  
 
 private function getfilepath(fullpath)
 if fullpath <> "" then
  getfilepath = left(fullpath,instrrev(fullpath, ""))
 else
  getfilepath = ""
 end if
 end function
 
 private function getfilename(fullpath)
 if fullpath <> "" then
  getfilename = mid(fullpath,instrrev(fullpath, "")+1)
 else
  getfilename = ""
 end if
 end function

end class

class fileinfo
 dim formname,filename,filepath,filesize,filetype,filestart
 private sub class_initialize 
  filename = ""
  filepath = ""
  filesize = 0
  filestart= 0
  formname = ""
  filetype = ""
 end sub
 
 public function saveas(fullpath)
  dim ofilestream,errorchar,i
  saveas=1
  if trim(fullpath)="" or right(fullpath,1)="/" then exit function
  set ofilestream=createobject("adodb.stream")
  ofilestream.type=1
  ofilestream.mode=3
  ofilestream.open
  oupfilestream.position=filestart
  oupfilestream.copyto ofilestream,filesize
  ofilestream.savetofile fullpath,2
  ofilestream.close
  set ofilestream=nothing 
  saveas=0
 end function
end class
</script>

此文所诉的内容是上传文件的最简化程式,请朋友们自己分析一下,学会本演练,asp一般的上传功能就基本掌握了

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网