摘录网络 asp 上传代码 支持大文件上传 asp上传图片代码
<%OptionExplicit%>
<%
'Code:200(上传成功)、504(文件为空)、502(上传上限)、501(表示网络中断)、500(上传错误)、
'505(文件类型禁止上传),506(未设置允许的文件后缀),507(未设置上传路径)
'--------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------
Server.ScriptTimeout=99999999
Response.Buffer=True
Response.Charset="utf-8"
ConstUPLOAD_PATH="/upload/"
ClassUpload_Cls
PrivateobjUpStream,objConvertStream,objFso
PrivatelngMaxSize,lngRequestBytes,lngReadChunkBytes
PrivatestrCode
PrivatestrFileSuffix,strFilename,strAllowSuffix,strFilePath,lngFileSize,i
PrivateSubClass_Initialize()
Code=0
MaxSize=0
lngReadChunkBytes=131072'64KB
lngRequestBytes=CLng(Request.TotalBytes)
EndSub
PrivateSubClass_Terminate()
IfIsObject(objUpStream)Then
objUpStream.Close
SetobjUpStream=Nothing
SetobjConvertStream=Nothing
Endif
EndSub
'------
'定义允许上传的文件格式,用英文逗号隔开(,)形如:gif,jpg
'------
PublicPropertyLetAllowSuffix(strVal)
strAllowSuffix=LCase(strVal)
EndProperty
PublicPropertyGetFileName()
FileName=strFileName
EndProperty
PublicPropertyLetFileName(strVal)
strFileName=strVal
EndProperty
'------
'获取上传的文件后缀名
'------
PublicPropertyGetFileSuffix()
FileSuffix=strFileSuffix
EndProperty
PublicPropertyLetFileSuffix(strVal)
strFileSuffix=strVal
EndProperty
'------
'获取上传文件大小单位:KB
'------
PublicPropertyGetFileSize()
FileSize=lngFileSize
EndProperty
PrivatePropertyLetFileSize(lngVal)
lngFileSize=lngVal
EndProperty
'-------
'获取上传文件相对路径
'-------
PublicPropertyGetFilePath()
FilePath=strFilePath
EndProperty
'-------
'设置上传文件路径
'-------
PublicSubSetFilePath(strCurpath,n)
DimintRnd
SelectCasen
CaseElse
Randomize
intRnd=Int(9000*Rnd)
Randomize
intRnd=intRnd+Int(100*Rnd)
strFilePath=strCurpath&FormatTimePattern(Now(),"ymd")&"/"
EndSelect
CallCreateFolder(Server.MapPath(FilePath))
EndSub
'------
Rem根据时间生成的文件名
'------
PublicFunctionAutoFilePath()
AutoFilePath=FilePath&FileName
EndFunction
PublicFunctionFormatTimePattern(dateSTime,strPattern)
DimdateTime,strVal
dateTime=dateSTime
strVal=strPattern
strVal=Replace(strVal,"y",Year(dateTime))
strVal=Replace(strVal,"m",Fill0Char(Month(dateTime)))
strVal=Replace(strVal,"d",Fill0Char(Day(dateTime)))
strVal=Replace(strVal,"h",Fill0Char(Hour(dateTime)))
strVal=Replace(strVal,"n",Fill0Char(Minute(dateTime)))
strVal=Replace(strVal,"s",Fill0Char(Second(dateTime)))
FormatTimePattern=strVal
EndFunction
PrivateFunctionFill0Char(strVal)
IfLen(strVal)<2Then
Fill0Char="0"&strVal
Else
Fill0Char=strVal
EndIf
EndFunction
'------
'设置上传文件大小上限(单位:Bytes),默认4096KB
'------
PublicPropertyLetMaxSize(lngVal)
lngMaxSize=FormatNum(lngVal,4194304)
EndProperty
'------
'返回上传状态代码
'------
PublicPropertyGetCode()
Code=strCode
EndProperty
PrivatePropertyLetCode(strVal)
strCode=strVal
EndProperty
PublicSubGetFileData()
DimbinCrLf,binBoundary
DimlngFirstBoundary,lngEndBoundary
DimstrInfoHeader,strFieldName,strContentType,strItemValue
DimobjFileStream
DimintPos'临时位置
strContentType=""
IflngRequestBytes<1Then
Code=504
ExitSub
EndIf
IflngMaxSize<>0AndlngRequestBytes>lngMaxSizeThen
Code=502
ExitSub
EndIf
SetobjUpStream=Server.CreateObject("Adodb.Stream")
objUpStream.Mode=3
objUpStream.Type=1
objUpStream.Open
SetobjConvertStream=Server.CreateObject("adodb.stream")
objConvertStream.Mode=3
objConvertStream.Charset="utf-8"
binCrLf=ChrB(13)&ChrB(10)'换行符
lngFirstBoundary=ParseChunk(objUpStream,binCrLf,1)
binBoundary=SubBinString(1,lngFirstBoundary-1)'取得边界串
SetobjFileStream=NewFileInfo_Cls
DoWhileStrComp(SubBinString(lngFirstBoundary,2),binCrLf)=0
lngFirstBoundary=lngFirstBoundary+2
RemBegin分解表单项目
IfstrContentType=""Then
DoWhileTrue
lngEndBoundary=ParseChunk(objUpStream,binCrLf,lngFirstBoundary)
strInfoHeader=SubString(lngFirstBoundary,lngEndBoundary-lngFirstBoundary)
lngFirstBoundary=lngEndBoundary+2
intPos=InStr(strInfoHeader,":")
IfintPos=0ThenExitDo
IfintPos>0Then
IfStrComp(Left(strInfoHeader,intPos-1),"Content-Disposition",1)=0Then
'取表单项名称
strFieldName=ExtractValue(strInfoHeader,intPos+1,"name")
'取文件路径,取文件名称
FileName=ExtractFileName(ExtractValue(strInfoHeader,intPos+1,"filename"))
IfNotCheckSuffix(FileName)ThenExitSub
ElseIfStrComp(left(strInfoHeader,intPos-1),"Content-Type",1)=0Then
'取文件类型
strContentType=Trim(Mid(strInfoHeader,intPos+1))
EndIf
EndIf
Loop
EndIf
RemEnd表单项目结束
IfFileName<>""Then
'掐头后移入流对象
MoveStreamobjUpStream,objFileStream.Stream,lngFirstBoundary
lngEndBoundary=ParseChunk(objFileStream.Stream,binBoundary&binCrLf,1)
'流对象去尾
MoveStreamobjFileStream.Stream,objUpStream,lngEndBoundary
lngFirstBoundary=lngFirstBoundary+2+LenB(binBoundary)
Else
lngEndBoundary=ParseChunk(objUpStream,binBoundary,lngFirstBoundary)
strItemValue=SubString(lngFirstBoundary,lngEndBoundary-2-lngFirstBoundary)
'移动位置
lngFirstBoundary=lngEndBoundary+LenB(binBoundary)
EndIf
Loop
Code=200
FileSize=objFileStream.FileSize
SetobjFso=Server.CreateObject("Scripting.FileSystemObject")
CallCheckFile
SetobjFso=Nothing
objFileStream.SaveAs(Server.MapPath(AutoFilePath))
SetobjFileStream=Nothing
EndSub
PrivateSubCheckFile()
IfobjFso.FileExists(Server.MapPath(AutoFilePath))Then
FileName="[1]"&strFileName
CallCheckFile
EndIf
EndSub
PrivateFunctionExtractValue(strString,startPos,strName)
DimstrVal
DimstrCurPos,intCurPos
Dimn1,n2
strVal=strString
strCurPos=strName&"="""
intCurPos=InStr(startPos,strVal,strCurPos)
IfintCurPos>0Then
n1=intCurPos+Len(strCurPos)
n2=InStr(n1,strVal,"""")
ifn2>n1thenExtractValue=Mid(strVal,n1,n2-n1)
EndIf
EndFunction
PrivateFunctionSubBinString(StartPos,ReadLen)
DimlngStartPos,lngReadLen
DimbinBoundary
lngReadLen=ReadLen
IflngReadLen=0ThenSubBinString="":ExitFunction
lngStartPos=StartPos
IfobjUpStream.Size<lngStartPos+lngReadLen-1ThenReadChunk2StreamobjUpStream
objUpStream.Position=lngStartPos-1
binBoundary=objUpStream.Read(lngReadLen)
SubBinString=MidB(binBoundary,1)
EndFunction
PrivateFunctionSubString(StartPos,ReadLen)
DimlngStartPos,lngReadLen
DimbinBoundary
lngReadLen=ReadLen
IflngReadLen=0ThenSubString="":ExitFunction
lngStartPos=StartPos
IfobjUpStream.Size<(lngStartPos+lngReadLen-1)ThenReadChunk2StreamobjUpStream
objUpStream.Position=lngStartPos-1
binBoundary=objUpStream.Read(lngReadLen)
WithobjConvertStream
.Type=1
.Open
.WritebinBoundary
.Position=0
.Type=2
SubString=.ReadText
.Close
EndWith
EndFunction
Rem解析一个块
PrivateFunctionParseChunk(obj,Boundary,StartPos)
'读取块的起始位置,找到边界的位置(0表示没有)
DimlngStartPos,lngFoundPos,lngBoundaryLen
DimbinChunk
lngStartPos=StartPos
lngFoundPos=0
lngBoundaryLen=LenB(Boundary)
DoWhilelngFoundPos=0
'数据流长度不够时,读取一个块(lngReadChunkBytes)
Ifobj.Size<(lngStartPos+lngBoundaryLen-1)ThenReadChunk2Streamobj
obj.Position=lngStartPos-1
binChunk=obj.Read
lngFoundPos=InstrB(binChunk,Boundary)
'未找到边界,则向后移动一个位置
IflngFoundPos=0ThenlngStartPos=lngStartPos+LenB(binChunk)-lngBoundaryLen+1
Loop
ParseChunk=lngStartpos+lngFoundPos-1
EndFunction
PrivateSubReadChunk2Stream(obj)
IfResponse.IsClientConnected=FalsethenCode=501:ExitSub
obj.Position=obj.Size
obj.WriteRequest.BinaryRead(lngReadChunkBytes)
EndSub
PrivateSubMoveStream(FromStream,ToStream,StartPos)
FromStream.Position=StartPos-1
ToStream.Position=ToStream.Size
FromStream.CopyToToStream
FromStream.Position=StartPos-1
'将流对象的结束设定到当前位置
FromStream.SetEOS
EndSub
PrivateFunctionExtractFileName(strString)
DimstrVal
strVal=Replace(strString,Chr(0),"")
strVal=Replace(strVal,"","")
strVal=Replace(strVal,"..","")
strVal=Replace(strVal,"'","")
strVal=Replace(strVal,"[","")
strVal=Replace(strVal,"]","")
strVal=Replace(strVal,"<","")
strVal=Replace(strVal,">","")
strVal=Replace(strVal,"*","")
strVal=Replace(strVal,"&","")
ExtractFileName=Mid(strVal,InStrRev(strVal,"")+1)
EndFunction
PrivateFunctionCheckSuffix(strA)
DimstrSeparate,strTempFileSuffix
CheckSuffix=True
strSeparate=",asp,asa,cer,aspx,php,cdx,htr,shtm,shtml,stm,idc,"
FileSuffix=LCase(Mid(strA,InStrRev(strA,".")+1))
strTempFileSuffix=","&strFileSuffix&","
IfInStr(strSeparate,strTempFileSuffix)>0Then
Code=505
CheckSuffix=False
ExitFunction
EndIf
EndFunction
PrivateFunctionCreateFolder(strFolderPath)
DimsPath,i,strTempPath,n,objFso,RootPath
CreateFolder=False
SetobjFso=Server.CreateObject("Scripting.FileSystemobject")
IfobjFso.FolderExists(strFolderPath)Then
CreateFolder=True
ExitFunction
EndIf
RootPath=Server.MapPath("/")&""
sPath=Split(strFolderPath,"")
strTempPath=""
n=UBound(Split(RootPath,""))
Fori=nToUBound(sPath)
strTempPath=strTempPath&sPath(i)&""
IfNotobjFso.FolderExists(RootPath&strTempPath)Then
objFso.CreateFolder(RootPath&strTempPath)
EndIf
Next
SetobjFso=Nothing
IfErr=0Then
CreateFolder=True
Else
CreateFolder=False
EndIf
EndFunction
PrivateFunctionFormatNum(intVal,DefaultVal)
IfNotIsNumeric(intVal)Then
FormatNum=Clng(DefaultVal)
Else
FormatNum=Clng(intVal)
EndIf
EndFunction
EndClass
ClassFileInfo_Cls
PrivateobjFileStream
PublicFunctionFileSize()
DimlngSize
lngSize=objFileStream.Size
FileSize=CLng(lngSize/1024)
EndFunction
PublicPropertyGetStream()
SetStream=objFileStream
EndProperty
PublicSubSaveAs(strFilePath)
OnErrorResumeNext
objFileStream.SaveToFilestrFilePath,2
IfErr.Number>0ThenResponse.Write"UploadErr:"&Err.Description&"<br>":ExitSub
EndSub
PrivateSubClass_Initialize
SetobjFileStream=CreateObject("Adodb.Stream")
objFileStream.Mode=3
objFileStream.Type=1
objFileStream.Open
Endsub
PrivateSubClass_Terminate
objFileStream.Close
SetobjFileStream=Nothing
Endsub
EndClass
Dimaction
DimobjUpload,objFile
action=Trim(Request.QueryString("action"))
Response.Write"<br>"
SelectCaseaction
Case"save"
CallSave
Case"list"
CallFileList
CaseElse
CallHeader
EndSelect
SubSave()
Dimstime,etime
stime=Timer
SetobjUpload=NewUpload_Cls
objUpload.AllowSuffix=""
objUpload.MaxSize=0
objUpload.SetFilePathUPLOAD_PATH,1
CallobjUpload.GetFileData
etime=Timer
Response.Write"上传执行代码Code:"&objUpload.Code&"<br>"
Response.Write"文件路径:"&objUpload.AutoFilePath&"<br>"
Response.Write"文件大小:"&objUpload.FileSize&"KB<br>"
Response.Write"执行时间:"&FormatNumber((etime-stime),2)&"second"
SetobjUpload=Nothing
CallHeader
EndSub
SubHeader()
%>
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8">
<title></title>
<tablewidth="600"border="0"cellspacing="0"cellpadding="0">
<br><br><br><br><br>
'Code:200(上传成功)、504(文件为空)、502(上传上限)、501(表示网络中断)、500(上传错误)、
'505(文件类型禁止上传),506(未设置允许的文件后缀),507(未设置上传路径)
<formaction="?action=save"method="post"enctype="multipart/form-data"name="form1">
<tralign="center">
<tdwidth="80"height="46">选择地址:</td>
<tdwidth="300">
<inputname="uploadfile"type="file"id="uploadfile"size="30"></td>
<tdwidth="60"><inputtype="submit"name="Submit"value="上传"></td>
</tr>
<tralign="center">
<tdheight="20"colspan="3"><inputtype="button"name="Submit"value="关闭"onClick="window.close();"></td>
</tr>
</form>
</table>
<%
EndSub
SubFileList()
DimstrPath
DimobjFso,objUpload
DimobjFolders,objFiles,Folder,File
DimstrThisPath
SetobjUpload=NewUpload_Cls
strPath=Trim(Request.QueryString("path"))
IfLen(strPath)=0Then
strThisPath=UPLOAD_PATH&objUpload.FormatTimePattern(Now(),"ymd")&"/"
ElseIfCStr(LCase(strPath))="/upload/"Then
strThisPath=UPLOAD_PATH
Else
strThisPath=UPLOAD_PATH&strPath
EndIf
%>
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8">
<title></title>
<br>
<tablewidth="780"align="center"border="0"cellspacing="1"cellpadding="0"bgcolor="#CCCCCC">
<ahref="?action=list&path=/upload/">返回上一个路径:</a>
<br><br>
当前路径:<%Response.WritestrThisPath%>
<br><br>
<trbgcolor="#EFEFEF">
<tdalign="center">文件夹/文件名</a></td>
<tdalign="center"height="30">大小</td>
<tdalign="center">文件类型</td>
<tdalign="center">创建时间</td>
<tdalign="center">修改时间</td>
</tr>
<%
SetobjFso=Server.CreateObject("Scripting.FileSystemObject")
SetobjFolders=objFso.GetFolder(Server.MapPath(strThisPath))
ForEachFolderInobjFolders.subFolders
%>
<trbgcolor="#FFFFFF">
<tdheight="30"><ahref="?action=list&path=<%=Folder.Name%>"><%=Folder.Name%></a></td>
<tdalign="center"><%=FileSize(Folder.Size)%></td>
<tdalign="center"><%=Folder.Type%></td>
<tdalign="center"><atitle="创建时间"><%=objUpload.FormatTimePattern(Folder.DateCreated,"y-m-dh:n:s")%></a></td><td><atitle="修改时间"><%=objUpload.FormatTimePattern(Folder.DateLastModified,"y-m-dh:n:s")%></a></td>
</tr>
<%
Next
ForEachFileInobjFolders.Files
%>
<trbgcolor="#FFFFFF">
<tdheight="30"><ahref="<%=strThisPath&"/"&File.Name%>"><%=File.Name%></a></td>
<tdalign="center"><%=FileSize(File.Size)%></td>
<tdalign="center"><%=File.Type%></td>
<tdalign="center"><atitle="创建时间"><%=objUpload.FormatTimePattern(File.DateCreated,"y-m-dh:n:s")%></a></td><td><atitle="修改时间"><%=objUpload.FormatTimePattern(File.DateLastModified,"y-m-dh:n:s")%></a></td>
</tr>
<%
Next
SetobjFolders=Nothing
SetobjFso=Nothing
SetobjUpload=Nothing
%>
</table>
</body>
</html>
<%
EndSub
FunctionFileSize(intSize)
FileSize=CLng(intSize/1024)&"K"
EndFunction
%>
更多阅读
怎么在百度上上传图片,百度上传图片的方法 怎么在百度上传图片
怎么在百度上上传图片,百度上传图片的方法——简介现在在网络用户如果出现什么问题,都喜欢用百度在百度上查找参考答案,小编也是这样做的。可是能不能在百度上查找自己的图片呢?那么就需要在百度上上传图片。小编就教教大家怎么在百度上
百度网盘怎么用迅雷下载大文件 百度云的资源在哪里找
百度网盘怎么用迅雷下载大文件——简介现在百度云资源多多,很多大文件下载想用迅雷下载该怎么下载呢?百度网盘怎么用迅雷下载大文件——工具/原料自己的百度网盘别人分享的大文件地址百度网盘怎么用迅雷下载大文件——方法/步骤
怎么上传图片到淘宝 淘宝店怎么上传商品
怎么上传图片到淘宝——简介最近有很多新开淘宝店铺问我怎么把图片上传到淘宝上去?小编专门整理一篇经验希望能够帮到大家。怎么上传图片到淘宝——工具/原料淘宝账号怎么上传图片到淘宝——方法/步骤怎么上传图片到淘宝 1、打开百
WiFi传送大文件零流量闪传 同一wifi下传送文件
WiFi传送大文件(零流量闪传)——简介手机与电脑传输信息的速度最快、传输最远,选Wifi是不错的选择。蓝牙 VS WiFi 就像是 拿鸡蛋碰石头。电脑可以在单机状态与手机传输文件,零流量闪传。WiFi传送大文件(零流量闪传)——工具/原料电脑Win
怎样上传图片 怎样在淘宝上上传照片
怎样上传图片——简介我们在生活中有很生活中的美好图片需要上传到网上与大家一起分享,最常见的是在QQ空间里,怎样上传图片呢?下面给大家介绍一下,以QQ空间为例,其它的相同。怎样上传图片——方法/步骤怎样上传图片 1、打开QQ空间,