asp文件上传(asp文件上传与下载)

http://www.itjxue.com  2023-01-28 21:30  来源:未知  点击次数: 

asp程序如何上传?

分类: 电脑/网络 程序设计 其他编程语言

问题描述:

asp程序如何上传?

解析:

先给你个无组件UPLAOD 的类给你。包括说明 至于删除使用FSO的 DELETEFILE就可以了

文件:upload_5xsoft.inc

!--METADATA TYPE="TypeLib" UUID="{***********-0000-0010-8000-00aa006d2ea4}"--

%

dim fo,gPath,gAct,gFileName,gFilePath,g,gRootUrl,gBaseUrl,gPage,gSearch,gEachPage,gFolder,gFilter

dim gPageSize,sTemp,gNoFile

dim sfor(28,2)

gPageSize=100

gRootUrl=Session("webeditbase")

if right(gRootUrl,1)"/" then gRootUrl=gRootUrl"/"

gNoFile=array("asp","aspx") '禁止访问的脚本

set fo=Server.CreateObject("Scripting.FileSystemObject")

sFor(0,0)="txt":sFor(0,1)=1

sFor(1,0)="chm":sFor(1,1)=2

sFor(2,0)="hlp":sFor(2,1)=2

sFor(3,0)="doc":sFor(3,1)=3

sFor(4,0)="pdf":sFor(4,1)=4

sFor(5,0)="gif":sFor(5,1)=6

sFor(6,0)="":sFor(6,1)=6

sFor(7,0)="png":sFor(7,1)=6

sFor(8,0)="bmp":sFor(8,1)=6

sFor(9,0)="asp":sFor(9,1)=7

sFor(10,0)="jsp":sFor(10,1)=7

sFor(11,0)="js" :sFor(11,1)=7

sFor(12,0)="":sFor(12,1)=8

sFor(13,0)="":sFor(13,1)=8

sFor(14,0)="s":sFor(14,1)=8

sFor(15,0)="zip":sFor(15,1)=9

sFor(16,0)="rar":sFor(16,1)=9

sFor(17,0)="exe":sFor(17,1)=10

sFor(18,0)="avi":sFor(18,1)=11

sFor(19,0)="mpg":sFor(19,1)=11

sFor(20,0)="ra" :sFor(20,1)=12

sFor(21,0)="ram":sFor(21,1)=12

sFor(22,0)="mid":sFor(22,1)=13

sFor(23,0)="wav":sFor(23,1)=13

sFor(24,0)="mp3":sFor(24,1)=13

sFor(25,0)="asf":sFor(25,1)=11

sFor(26,0)="php":sFor(26,1)=7

sFor(27,0)="php3":sFor(27,1)=7

sFor(28,0)="aspx":sFor(28,1)=7

gFilePath=Request.ServerVariables("SCRIPT_NAME")

gFileName=mid(gFilePath,instrRev(gFilePath,"/")+1)

gFilePath=lcase(left(gFilePath,instrRev(gFilePath,"/")))

gPath=lcase(getVar("path","str",gRootUrl))

if left(gPath,1)"/" then gPath=gRootUrlgPath

gPage=getVar("page","num",1)

gAct=getVar("act","str","")

gFilter=getVar("filter","str","")

setBaseUrl

sub setBaseUrl()

gBaseUrl=gFileName"?page="gPage"path="gPath

if gFilter"" then gBaseUrl=gBaseUrl"filter="gFilter

end sub

function getVar(theStr,strType,defValue)

select case strType

case "str"

if isEmpty(Request.QueryString(theStr)) or trim(Request.QueryString(theStr))="" then

getVar=defValue

else

getVar=trim(Request.QueryString(theStr))

end if

case "num"

if isEmpty(Request.QueryString(theStr)) or not isNumeric(Request.QueryString(theStr)) then

getVar=defValue

else

getVar=cint(Request.QueryString(theStr))

end if

case else

getStr=defValue

end select

end function

function getForm(theStr,strType,defValue)

select case strType

case "str"

if isEmpty(Request.form(theStr)) or trim(Request.form(theStr))="" then

getForm=defValue

else

getForm=trim(Request.form(theStr))

end if

case "num"

if isEmpty(Request.form(theStr)) or not isNumeric(Request.form(theStr)) then

getForm=defValue

else

getForm=cint(Request.form(theStr))

end if

case else

getForm=defValue

end select

end function

sub End(info,i *** ack,dir)

set fo=nothing

set gFolder=nothing

if info"" then Response.Write("script language=""javascript""alert('"info"');/script")

select case i *** ack

case 1

Response.Write("script language=""javascript""history.back();/script")

case 2

Response.Write("script language=""javascript""location.href='"dir"';/script")

end select

Response.write("/body/")

Response.End

end sub

function procIsEdit(sName)

dim i1,i,isEdit

isEdit=Array(1,7,8)

procIsEdit=0

i1=procGetFormat(sName)

for i=0 to ubound(isEdit)

if isEdit(i)=i1 then

procIsEdit=1

exit for

end if

next

end function

function procGetExtName(sName)

procGetExtName=""

if instrRev(sName,".")1 then exit function

procGetExtName=lcase(mid(sName,instrRev(sName,".")+1))

end function

function procGetFormat(sName)

dim i,str

procGetFormat=0

if instrRev(sName,".")=0 then exit function

str=lcase(mid(sName,instrRev(sName,".")+1))

for i=0 to uBound(sFor,1)

if str=sFor(i,0) then

procGetFormat=sFor(i,1)

exit for

end if

next

end function

function procCheckFile(sName,ischeck)

dim sExt,sPath,i,errorchar

errorchar=array("'","""","\","/","*","?","","|","","")

procCheckFile="ok"

sExt=lcase(procGetExtName(sName))

if gFilter"" then

if instr(sName,gFilter)1 then

procCheckFile="没有权限访问此文件!"

exit function

end if

end if

if Session("webeditrun")="0" then

for i=0 to ubound(gNoFile)

if gNoFile(i)=sExt then

procCheckFile="没有权限访问此文件!"

exit function

end if

next

end if

if ischeck0 then

sPath=left(sName,instrRev(sName,"/"))

sPath=procCheckDir(sPath,1)

if sPath"ok" then

procCheckFile=sPath

exit function

end if

if ischeck=1 and not fo.FileExists(Server.MapPath(sName)) then

procCheckFile="文件没有找到!"

exit function

end if

else

for i=0 to ubound(errorchar)

if instr(sName,errorchar(i))0 then

procCheckFile="文件名中含有非法字符!"

exit function

end if

next

end if

procCheckFile="ok"

end function

function procCheckDir(sPath,mode)

dim errorchar,i,hd,str

sPath=lcase(sPath)

procCheckDir="ok"

errorchar=array("'","""","\","..","","*","?","","|","","")

if isempty(sPath) or trim(sPath)="" then

procCheckDir="目录不能为空!"

exit function

end if

for i=0 to ubound(errorchar)

if instr(sPath,errorchar(i))0 then

procCheckDir="目录名中含有非法字符"

exit function

end if

next

if gFilePath=left(sPath,len(gFilePath)) then

procCheckDir="没有权限访问此目录!"

exit function

end if

if mode=0 then exit function

if not fo.FolderExists(Server.MapPath(sPath)) then

procCheckDir="目录"sPath"没有找到!"

exit function

end if

if left(sPath,len(gRootUrl))gRootUrl then

procCheckDir="没有权限访问此目录!"

exit Function

end if

end function

'取文件夹的类型

Function GetFileExt(strFilePath)

GetFileExt=Mid(strFilePath,InstrRev(strFilePath,"."))

End Function

%

SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT

'''''''''''''''''''''''''''''''''''''''''''''''''

'

'请保留此信息: 稻香老农制作5xSoft/ ;

'

'''''''''''''''''''''''''''''''''''''''''''''''''

dim upfile_5xSoft_Stream

Class upload_5xSoft

dim Form,File,Version

Private Sub Class_Initialize

dim iStart,iFileNameStart,iFileNameEnd,iEnd,vbEnter,iFormStart,iFormEnd,theFile

dim strDiv,mFormName,mFormValue,mFileName,mFileSize,mFilePath,iDivLen,mStr

Version="数据添加成功!!"

if Request.TotalBytes1 then Exit Sub

set Form=CreateObject("Scripting.Dictionary")

set File=CreateObject("Scripting.Dictionary")

set upfile_5xSoft_Stream=CreateObject("Adodb.Stream")

upfile_5xSoft_Stream.mode=3

upfile_5xSoft_Stream.type=1

upfile_5xSoft_Stream.open

upfile_5xSoft_Stream.write Request.BinaryRead(Request.TotalBytes)

vbEnter=Chr(13)Chr(10)

iDivLen=inString(1,vbEnter)+1

strDiv=subString(1,iDivLen)

iFormStart=iDivLen

iFormEnd=inString(iformStart,strDiv)-1

while iFormStart iFormEnd

iStart=inString(iFormStart,"name=""")

iEnd=inString(iStart+6,"""")

mFormName=subString(iStart+6,iEnd-iStart-6)

iFileNameStart=inString(iEnd+1,"filename=""")

if iFileNameStart0 and iFileNameStartiFormEnd then

iFileNameEnd=inString(iFileNameStart+10,"""")

mFileName=subString(iFileNameStart+10,iFileNameEnd-iFileNameStart-10)

iStart=inString(iFileNameEnd+1,vbEntervbEnter)

iEnd=inString(iStart+4,vbEnterstrDiv)

if iEndiStart then

mFileSize=iEnd-iStart-4

else

mFileSize=0

end if

set theFile=new FileInfo

theFile.FileName=getFileName(mFileName)

theFile.FilePath=getFilePath(mFileName)

theFile.FileSize=mFileSize

theFile.FileStart=iStart+4

theFile.FormName=FormName

file.add mFormName,theFile

else

iStart=inString(iEnd+1,vbEntervbEnter)

iEnd=inString(iStart+4,vbEnterstrDiv)

if iEndiStart then

mFormValue=subString(iStart+4,iEnd-iStart-4)

else

mFormValue=""

end if

form.Add mFormName,mFormValue

end if

iFormStart=iformEnd+iDivLen

iFormEnd=inString(iformStart,strDiv)-1

wend

End Sub

Private Function subString(theStart,theLen)

dim i,c,stemp

upfile_5xSoft_Stream.Position=theStart-1

stemp=""

for i=1 to theLen

if upfile_5xSoft_Stream.EOS then Exit for

c=ascB(upfile_5xSoft_Stream.Read(1))

If c 127 Then

if upfile_5xSoft_Stream.EOS then Exit for

stemp=stempChr(AscW(ChrB(AscB(upfile_5xSoft_Stream.Read(1)))ChrB(c)))

i=i+1

else

stemp=stempChr(c)

End If

Next

subString=stemp

End function

Private Function inString(theStart,varStr)

dim i,j,bt,theLen,str

InString=0

Str=toByte(varStr)

theLen=LenB(Str)

for i=theStart to upfile_5xSoft_Stream.Size-theLen

if iupfile_5xSoft_Stream.size then exit Function

upfile_5xSoft_Stream.Position=i-1

if AscB(upfile_5xSoft_Stream.Read(1))=AscB(midB(Str,1)) then

InString=i

for j=2 to theLen

if upfile_5xSoft_Stream.EOS then

inString=0

Exit for

end if

if AscB(upfile_5xSoft_Stream.Read(1))AscB(MidB(Str,j,1)) then

InString=0

Exit For

end if

next

if InString0 then Exit Function

end if

next

End Function

Private Sub Class_Terminate

form.RemoveAll

file.RemoveAll

set form=nothing

set file=nothing

upfile_5xSoft_Stream.close

set upfile_5xSoft_Stream=nothing

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

Private function toByte(Str)

dim i,iCode,c,iLow,iHigh

toByte=""

For i=1 To Len(Str)

c=mid(Str,i,1)

iCode =Asc(c)

If iCode0 Then iCode = iCode + 65535

If iCode255 Then

iLow = Left(Hex(Asc(c)),2)

iHigh =Right(Hex(Asc(c)),2)

toByte = toByte chrB("H"iLow) chrB("H"iHigh)

Else

toByte = toByte chrB(AscB(c))

End If

Next

End function

End Class

Class FileInfo

dim FormName,FileName,FilePath,FileSize,FileStart

Private Sub Class_Initialize

FileName = ""

FilePath = ""

FileSize = 0

FileStart= 0

FormName = ""

End Sub

Public function SaveAs(FullPath)

dim dr,ErrorChar,i

SaveAs=1

if trim(fullpath)="" or FileSize=0 or FileStart=0 or FileName="" then exit function

if FileStart=0 or right(fullpath,1)="/" then exit function

set dr=CreateObject("Adodb.Stream")

dr.Mode=3

dr.Type=1

dr.Open

upfile_5xSoft_Stream.position=FileStart-1

upfile_5xSoft_Stream.copyto dr,FileSize

dr.SaveToFile FullPath,2

dr.Close

set dr=nothing

SaveAs=0

end function

End Class

/SCRIPT

上传表单:

form action="upload" method="post" enctype="multipart/form-data" name=form1

上传:input name=file type=file size="30"

input type=submit

/form

文件upload

!--#include FILE="upload_5xsoft.inc"--

%

formPath="c:\upload\"

set upload=new upload_5xSoft

for each formName in upload.file

set file=upload.file(formName)

if file.FileSize0 then

response.write "br"file.FilePathfile.FileName",大小:"file.FileSize" = "formPathfile.FileName

file.SaveAs Server.mappath(formPathfile.FileName)

end if

next

%

asp文件上传:文件上传 源代码

! #include FILE="upload inc" % dim upload file formName formPath iCount filename fileExt set upload=new upload_ xSoft 建立上传对象 formPath="uploadimages/" ? 在目录后加(/) if right(formPath )"/" then formPath=formPath"/" iCount= for each formName in upload file 列出所有上传了的文件 ?set file=upload file(formName)? 生成一个文件对象 ?if file filesize then ? response write "font size= br请先选择你要上传的图片 [ a href=# onclick=history go( )重新上传/a ]/font" ?response end ?end if ? ?if file filesize then ? response write "font size= br图片大小超过了限制 [ a href=# onclick=history go( )重新上传/a ]/font" ?response end ?end if

fileExt=lcase(right(file filename ))

if fileEXT" jpg" and fileEXT" gif" then ? response write "font size= br文件格式只能为jpg和gif格式 [ a href=# onclick=history go( )重新上传/a ]/font" ?response end ?end if ?randomize ?ranNum=int( *rnd)+ ?filename=formPathyear(now)month(now)day(now)hour(now)minute(now)second(now)fileExt ?filename =year(now)month(now)day(now)hour(now)minute(now)second(now)fileExt ? ?if file FileSize then???????? 如果 FileSize 说明有文件数据 ?file SaveAs Server mappath(filename)?? 保存文件 response write "scriptparent form img value= "FileName " /script"

iCount=iCount+ ?end if ?set file=nothing next set upload=nothing? 删除此对象

Response Write "img src= "" /UpLoad/UpLoadImages/"FileName " onload = DrawImage(this) " ?response end %

下面是upload inc的代码

SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT dim upfile_ xSoft_Stream Class upload_ xSoft dim Form File Version Private Sub Class_Initialize dim iStart iFileNameStart iFileNameEnd iEnd vbEnter iFormStart iFormEnd theFile dim strDiv mFormName mFormValue mFileName mFileSize mFilePath iDivLen mStr if Request TotalBytes then Exit Sub set Form=CreateObject("Scripting Dictionary") set File=CreateObject("Scripting Dictionary") set upfile_ xSoft_Stream=CreateObject("Adodb Stream") upfile_ xSoft_Stream mode= upfile_ xSoft_Stream type= upfile_ xSoft_Stream open upfile_ xSoft_Stream write Request BinaryRead(Request TotalBytes) vbEnter=Chr( )Chr( ) iDivLen=inString( vbEnter)+ strDiv=subString( iDivLen) iFormStart=iDivLen iFormEnd=inString(iformStart strDiv) while iFormStart iFormEnd ? iStart=inString(iFormStart "name=""") ? iEnd=inString(iStart+ """") ? mFormName=subString(iStart+ iEnd iStart ) ? iFileNameStart=inString(iEnd+ "filename=""") ? if iFileNameStart and iFileNameStartiFormEnd then ?? iFileNameEnd=inString(iFileNameStart+ """") ?? mFileName=subString(iFileNameStart+ iFileNameEnd iFileNameStart ) ?? iStart=inString(iFileNameEnd+ vbEntervbEnter) ?? iEnd=inString(iStart+ vbEnterstrDiv) ?? if iEndiStart then ??? mFileSize=iEnd iStart ?? else ??? mFileSize= ?? end if ?? set theFile=new FileInfo ?? theFile FileName=getFileName(mFileName) ?? theFile FilePath=getFilePath(mFileName) ?? theFile FileSize=mFileSize ?? theFile FileStart=iStart+ ?? theFile FormName=FormName ?? file add mFormName theFile ? else ?? iStart=inString(iEnd+ vbEntervbEnter) ?? iEnd=inString(iStart+ vbEnterstrDiv)

if iEndiStart then ??? mFormValue=subString(iStart+ iEnd iStart ) ?? else ??? mFormValue="" ?? end if ?? form Add mFormName mFormValue ? end if

iFormStart=iformEnd+iDivLen ? iFormEnd=inString(iformStart strDiv) wend End Sub

Private Function subString(theStart theLen) ?dim i c stemp ?upfile_ xSoft_Stream Position=theStart ?stemp="" ?for i= to theLen ?? if upfile_ xSoft_Stream EOS then Exit for ?? c=ascB(upfile_ xSoft_Stream Read( )) ?? If c Then ??? if upfile_ xSoft_Stream EOS then Exit for ??? stemp=stempChr(AscW(ChrB(AscB(upfile_ xSoft_Stream Read( )))ChrB(c))) ??? i=i+ ?? else ??? stemp=stempChr(c) ?? End If ?Next ?subString=stemp End function

Private Function inString(theStart varStr) ?dim i j bt theLen str ?InString= ?Str=toByte(varStr) ?theLen=LenB(Str) ?for i=theStart to upfile_ xSoft_Stream Size theLen ?? if iupfile_ xSoft_Stream size then exit Function ?? upfile_ xSoft_Stream Position=i ?? if AscB(upfile_ xSoft_Stream Read( ))=AscB(midB(Str )) then ??? InString=i ??? for j= to theLen ????? if upfile_ xSoft_Stream EOS then ??????? inString= ??????? Exit for ????? end if ????? if AscB(upfile_ xSoft_Stream Read( ))AscB(MidB(Str j )) then ??????? InString= ??????? Exit For ????? end if ??? next ??? if InString then Exit Function ?? end if ?next End Function

Private Sub Class_Terminate? ? form RemoveAll ? file RemoveAll ? set form=nothing ? set file=nothing ? upfile_ xSoft_Stream close ? set upfile_ xSoft_Stream=nothing 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 "")+ ) ? Else ?? GetFileName = "" ? End If ?End? function

Private function toByte(Str) ?? dim i iCode c iLow iHigh ?? toByte="" ?? For i= To Len(Str) ?? c=mid(Str i ) ?? iCode =Asc(c) ?? If iCode Then iCode = iCode + ?? If iCode Then ???? iLow = Left(Hex(Asc(c)) ) ???? iHigh =Right(Hex(Asc(c)) ) ???? toByte = toByte chrB("H"iLow) chrB("H"iHigh) ?? Else ???? toByte = toByte chrB(AscB(c)) ?? End If ?? Next ?End function End Class

lishixinzhi/Article/program/net/201311/14198

如何使用AspUpload组件上传文件

你好,试试以下的方法:一、摘要Asp组件有内置的、服务器安装时附带的,更多的是第三方提供的,今天来学习文件上传的其中一个组件aspupload组件使用方法。

二、aspupload组件的下载、安装或注册 1、asp组件的下载、安装

(1)可以从网上下载。

(2)直接双击后进行安装。

AspUpload组件下载2、asp上传组件的功能

a.限制上载文件的大小

b.设置用户的权限

c.修改文件属性

d.同时上载多个文件

e.能够将文件保存到数据库中

f.支持文件删除,自动生成与服务器上文件不同名的文件

g.拥有管理权限的用户甚至可以使用该控件进行远程注册

三、aspupload组件的简单应用

1、实例一(1.asp):通过代码实现三个文件的上传功能。

如下图所示:

(1)静态页面:1个表单,三个文件域,一个按钮,其中表单form的动作如下。 (2)其中客户端文件要注意几点:

* 文件上载提交表单(Form)的enctype必须指定为“multipart/form-data”

* 语句表示上载文件域,用户可以在该域中输入或选定文件。

* 传递一个参数act(名称可自己取),其值可以自己随便定,目的是触发上传事件。

(3)动态代码如下:

2、实例二(2.asp):修改程序1.asp,要求在上传文件后显示上传文件的文件名及大小。

增加如下代码: response.write("文件1是:

")response.write(upload.files(1).path)

response.write("文件2是:

")response.write(upload.files(2).path)

response.write("文件3是:

")

response.write(upload.files(3).path)

说明:

upload.files方法用来获取文件的相关属性,path是文件的路径,size是文件的大小。

3、实例三(3.asp):修改程序2.asp,要求上传的三个文件大小不能超过5K,如果上传的文件已经存在则要求不覆盖文件。

在上传之前增加如下代码:

upload.setmaxsize 5120,false

upload.overwritefiles=fals说明:

(1)upload.setmaxsize 5120,false 其功能为设置文件最大为5120字节,false参数说明当文件超过5120字节时则删除超过部分,true参数说明当文件超过5120字节时则出错。

(2)upload.overwritefiles=false,其功能表示文件不进行覆盖,如果上传同样文件名的文件,上传后文件名自动会在后面添加一个数字。

四、自学第二个上传文件的组件

1、Lyfupload组件的下载

2、学习此组件的安装或注册

3、通过课本例子进行文件的上传

五、问题

1、传到学校里服务器172.18.0.7运行时出现以下错误,Server.CreateObject 失败

分析原因:学校服务器不支持aspupload上传组件

2、如果服务器不支持aspupload等上传组件,请大家使用无组件上传功能(编写代码),见书本上P322,此类代码比较复杂,同学们能够拿来使用,无须自己编写。

3、大家在网上申请个人空间时要看清服务器支持哪些组件,这样有利于编写代码。

(责任编辑:IT教学网)

更多

推荐linux服务器文章