asp将本地的文件上传到服务器
今天我们讲解如何利用asp的上传功能将本地的文件上传到服务器上。
最简系统包括下面三个文件:
upload.htm --上传口文件,选择本地文件
uploadimg.asp --上传程序控制文件
upload_5xsoft.inc --无组件上传类,此文件初学者不用学习,只要会用就可以了
upload.htm内容————上传口文件,选择本地文件
<html> <head> </head> <body> <tablewidth="80%"border="0"align="center"> <formname="form1"method="post"action="uploadimg.asp"enctype="multipart/form-data"> <tr> <tdalign="center"><inputname="upfile"type="file"id="upfile"></td> </tr> <tr> <tdalign="center"><inputtype="submit"name="Submit"value="上传图片"></td> </tr> </form> </table> </body> </html>
uploadimg.asp内容————上传程序控制文件
<!--#includeFILE="upload_5xsoft.inc"--> <% dimupload,file,filepath filepath="UPLOAD/" setupload=newupload_5xSoft''建立上传对象 foreachformNameinupload.file''列出所有上传了的文件 setfile=upload.file(formName)''生成一个文件对象 iffile.FileSize>0then''如果FileSize>0说明有文件数据 fname=file.filename file.SaveAsServer.mappath(filepath&fname)''保存文件 endif setfile=nothing next setupload=nothing''删除此对象
upload_5xsoft.inc内容————此文件内容不属于本演练程序内容,本演练应用此类的方法
<SCRIPTRUNAT=SERVERLANGUAGE=VBSCRIPT> dimoUpFileStream Classupload_5xSoft dimForm,File,Version PrivateSubClass_Initialize dimRequestBinDate,sStart,bCrLf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,oFileInfo dimiFileSize,sFilePath,sFileType,sFormvalue,sFileName dimiFindStart,iFindEnd dimiFormStart,iFormEnd,sFormName Version="无组件上传类Version0.93" setForm=Server.CreateObject("Scripting.Dictionary") setFile=Server.CreateObject("Scripting.Dictionary") ifRequest.TotalBytes<1thenExitSub settStream=Server.CreateObject("adodb.stream") setoUpFileStream=Server.CreateObject("adodb.stream") oUpFileStream.Type=1 oUpFileStream.Mode=3 oUpFileStream.Open oUpFileStream.WriteRequest.BinaryRead(Request.TotalBytes) Response.Write"<fontsize=""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.CopyTotStream,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) '如果是文件 ifInStr(45,sInfo,"filename=""",1)>0then setoFileInfo=newFileInfo '取得文件名 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.addsFormName,oFileInfo else '如果是表单项目 tStream.Close tStream.Type=1 tStream.Mode=3 tStream.Open oUpFileStream.Position=iInfoEnd oUpFileStream.CopyTotStream,iFormStart-iInfoEnd-3 tStream.Position=0 tStream.Type=2 tStream.Charset="gb2312" sFormvalue=tStream.ReadText form.AddsFormName,sFormvalue endif tStream.Close iFormStart=iFormStart+iStart+1 wend RequestBinDate="" settStream=nothing EndSub PrivateSubClass_Terminate ifnotRequest.TotalBytes<1then form.RemoveAll file.RemoveAll setform=nothing setfile=nothing oUpFileStream.Close setoUpFileStream=nothing endif EndSub PrivatefunctionGetFilePath(FullPath) IfFullPath<>""Then GetFilePath=left(FullPath,InStrRev(FullPath,"")) Else GetFilePath="" EndIf Endfunction PrivatefunctionGetFileName(FullPath) IfFullPath<>""Then GetFileName=mid(FullPath,InStrRev(FullPath,"")+1) Else GetFileName="" EndIf Endfunction EndClass ClassFileInfo dimFormName,FileName,FilePath,FileSize,FileType,FileStart PrivateSubClass_Initialize FileName="" FilePath="" FileSize=0 FileStart=0 FormName="" FileType="" EndSub PublicfunctionSaveAs(FullPath) dimoFileStream,ErrorChar,i SaveAs=1 iftrim(fullpath)=""orright(fullpath,1)="/"thenexitfunction setoFileStream=CreateObject("Adodb.Stream") oFileStream.Type=1 oFileStream.Mode=3 oFileStream.Open oUpFileStream.position=FileStart oUpFileStream.copytooFileStream,FileSize oFileStream.SaveToFileFullPath,2 oFileStream.Close setoFileStream=nothing SaveAs=0 endfunction EndClass </SCRIPT>
此文所诉的内容是上传文件的最简化程式,请朋友们自己分析一下,学会本演练,asp一般的上传功能就基本掌握了