日期:2014-05-01  浏览次数:21054 次

一段简单的ASP文件上传源代码示例,精简实用。

以下是upload.asp代码:
<form name="FORM" action="upload.asp" method="post">
  <input type="submit" name="submit" value="OK">
     <input type="file" name="file1" style="width:400"  value="">
  <input type=TEXT name=myface size=20 maxlength=100>
</form>
<%
if Request.Form("file1")<>"" then
Dim AllowExt_ ’允许上传类型(白名单)
Dim NoAllowExt_ ’不允许上传类型(黑名单)
NoAllowExt="exe;gif"  ’黑名单,可以在这里预设不可上传的文件类型,以文件的后缀名来判断,不分大小写,每个每缀名用;号分开,如果黑名单为空,则判断白名单
NoAllowExt=LCase(NoAllowExt)
AllowExt=""  ’白名单,可以在这里预设可上传的文件类型,以文件的后缀名来判断,不分大小写,每个后缀名用;号分开
AllowExt=LCase(AllowExt)

Function GetFileName(ByVal strFile)
  If strFile <> "" Then
   GetFileName = mid(strFile,InStrRev(strFile, "\")+1)
  Else
   GetFileName = ""
  End If
End  function
Function isAllowExt(Ext)
 if NoAllowExt="" then
  isAllowExt=cbool(InStr(1,";"&AllowExt&";",LCase(";"&Ext&";")))
 else
  isAllowExt=not CBool(InStr(1,";"&NoAllowExt&";",LCase(";"&Ext&";")))
 end if
End Function
Public Function GetFileExt(FullPath)
  If FullPath <> "" Then
    GetFileExt = LCase(Mid(FullPath,InStrRev(FullPath, ".")+1))
    Else
    GetFileExt = ""
  End If
End function

uppath="uploadfile\"
strFileName = Request.Form("file1")
strnum=trim(Len(strFileName))
filetype= mid(strFileName,strnum-2,3)
if isAllowExt(filetype) then
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Type = 1 ’ adTypeBinary
objStream.Open
objStream.LoadFromFile strFileName
objStream.SaveToFile Server.MapPath(uppath&GetFileName(strFileName)),2
objStream.Close
response.write "<script>parent.document.forms[0].myface.value=’" &uppath&"/"&GetFileName(strFileName)& "’</script>"
response.write "<a href=’" &uppath&GetFileName(strFileName)& "’>查看头像</a>"
response.write "文件类型:"&filetype&""
else
response.write"<SCRIPT language=JavaScript>alert(’头像文件只允许用GIF文件格式!’);"
response.write"this.location.href=’javascript:history.back();’</SCRIPT>"
response.end
end if
end if
%>