asp上传文件或图片

博客指出在form里执行resquest.conntent与上传图片功能不能放于同一form,有效方案是用两个form调用两个action,避免二进制与content组建冲突。还给出了上传图片的表单代码、功能扩展页代码及包含文件内容代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当在一个form里需要执行resquest.conntent时不能与上传图片的功能同时放在同一个form里,最有效的解决方案是使用两个form,调用两个action,这样不容易出现二进制与content组建冲突。实现代码如下:

<form method="post" action="submit4.asp" name="form2" enctype="multipart/form-data">
        <p align="left">   上传图片:
          <input type="file" name="file1" style="width:400" class="tx1" value="<%=image%>" size="50">
          <input type="submit" name="sub1" value="提交" >
          <input type="hidden" name="act" value="upload">
          <br>
          <font color="#990000"><br>
          &nbsp; <u>允许格式:gif|jpg|png</u>(仅限etime专访)</font> </p>
 </form>

此时调用的是submit4.asp这个功能扩展页,
下面是submit4这个页的可实现原码:


<!--#include FILE="upload_5xsoft.inc"-->
<%
FilePath="upfile/"
FileMaxSize="65000"
FileType=".jpg.gif.png"
fileweb="/"
%>
<% ''将当前的日期和时间转为文件名
function makefilename(fname)
  fname = now()
  fname = replace(fname,"-","")
  fname = replace(fname," ","")
  fname = replace(fname,":","")
  fname = replace(fname,"PM","")
  fname = replace(fname,"AM","")
  fname = replace(fname,"上午","")
  fname = replace(fname,"下午","")
  makefilename=fname
end function
%>
<%
dim upload,file,formName,iCount
set upload=new upload_5xSoft     ''建立上传对象
 UpFilePath ="upfile/"
 ''在目录后加(/)
 for each formName in upload.file ''列出所有上传了的文件
  set file=upload.file(formName)  ''生成一个文件对象
   if file.FileSize<FileMaxSize then ''如果未超过文件大小限制
      if instr(FileType,GetExtendName(file.fileName)) then
      vfname = makefilename(now())  '文件为允许的类型执行此行  
      fname=vfname & "." & GetExtendName(file.FileName)
   file.SaveAs Server.mappath(UpFilePath&fname)   ''保存文件
   'response.write fname
   'response.end
      'response.write  file.FilePath&file.FileName&" ("&file.FileSize&") => 上传文件成功! <br>"
   response.write"<script language='javascript'>{window.alert('"&file.FileName&"上传文件成功!');window.location.href='etime.asp?fname="&UpFilePath&fname&"'}</script>"
   else
    response.write "不支持所上传的类型!"
    response.write GetExtendName(file.fileName)
    response.write "<br>"
   end if
 else
         response.write "文件大小超出限制,您最多可以上传 "& FileMaxSize &"个字节的文件数据"
   exit for
   set file=nothing
 end if
  next
 set upload=nothing
    
  
    function GetExtendName(FileName)
        dim ExtName
        ExtName = LCase(FileName)
        ExtName = right(ExtName,3)
        ExtName = right(ExtName,3-Instr(ExtName,"."))
        GetExtendName = ExtName
    end function

%>
这里值得一提的是:
<!--#include FILE="upload_5xsoft.inc"-->
这是个包含文件内容代码为:(只要将下面代码放到记事本里并将其转变为文件名为upload_5xsoft并且扩展名为.inc即可使用!)

<SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT>

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="WLONG3D专用上传程序 Version 1.0"
if Request.TotalBytes<1 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 iFileNameStart>0 and iFileNameStart<iFormEnd then
   iFileNameEnd=inString(iFileNameStart+10,"""")
   mFileName=subString(iFileNameStart+10,iFileNameEnd-iFileNameStart-10)
   iStart=inString(iFileNameEnd+1,vbEnter&vbEnter)
   iEnd=inString(iStart+4,vbEnter&strDiv)
   if iEnd>iStart 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,vbEnter&vbEnter)
   iEnd=inString(iStart+4,vbEnter&strDiv)

   if iEnd>iStart 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=stemp&Chr(AscW(ChrB(AscB(upfile_5xSoft_Stream.Read(1)))&ChrB(c)))
    i=i+1
   else
    stemp=stemp&Chr(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 i>upfile_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 InString<>0 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 iCode<0 Then iCode = iCode + 65535
   If iCode>255 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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值