文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>ASP实现上传功能

ASP实现上传功能

时间:2009-01-12  来源:hkebao

1.用无组件上传方法
客户端提交的代码如下:
<form method="POST" action="../upfile_OtherBack.asp" enctype="multipart/form-data">
服务器端的保存操作代码如下:
call upload_0()
此过程的具体定义过程如下:
sub upload_0()    '使用化境无组件上传类
    set upload=new upload_file    '建立上传对象
    for each formName in upload.file '列出所有上传了的文件
        set file=upload.file(formName)  '生成一个文件对象
        if file.filesize<100 then
             msg="请先选择你要上传的文件!"
            founderr=true
        end if
        if file.filesize>(MaxFileSize*1024) then  '判断是否超过最大限制了
             msg="文件大小超过了限制,最大只能上传" & CStr(MaxFileSize) & "K的文件!"
            founderr=true
        end if
        fileExt=lcase(file.FileExt)
        Forumupload=split(UpFileType,"|")      '分割上传文件类型的
        for i=0 to ubound(Forumupload)
            if fileEXT=trim(Forumupload(i)) then
                EnableUpload=true
                exit for
            end if
        next
        if fileEXT="asp" or fileEXT="asa" or fileEXT="aspx" then
            EnableUpload=false
        end if
        if EnableUpload=false then
            'msg="这种文件类型不允许上传!\n\n只允许上传这几种文件类型:" & UpFileType
                        response.write""
         founderr=true
        end if
       
        strJS="<SCRIPT language=javascript>" & vbcrlf
        if founderr<>true then                '一切正常的情况
            randomize
            ranNum=int(900*rnd)+100
            filename=SavePath&year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&ranNum&"."&fileExt
            file.SaveToFile Server.mappath(FileName)   '保存文件,注意了这里的无组件上传的时候要区分大小写
            '保存到数据库中去
            '定义变量将其插入到数据库中去           
            dim elite,passed
            BigClassName = upload.form("select")   '通过upload进行提取表单中内容
            'response.Write(BigClassName+"大类的")
            SmallClassName = upload.form("smallclassid")
            'response.Write(SmallClassName+"小类的")
            Title = upload.form("Title")
            Spec = upload.form("Spec")
            Product_Id = upload.form("Product_Id")
            Buy_Price = upload.form("buy_price")
            product_Gain = upload.form("product_gain")
            price = upload.form("price")
            unit = upload.form("unit")
            memo = upload.form("memo")
            updatetime = upload.form("UpdateTime")       
            'response.Write(request.form("elite"))
            if upload.form("elite")="0" then
                elite = 1
            else
                elite = 0
            end if
           
            if upload.form("passed") = "0" then
                passed = 1
            else
                passed = 0
            end if           
            content = upload.form("content")
            goodsPic = filename                        '完整目录是 UploadFiles/20081127152854618.jpg               
            sql = "insert into product(BigClassName,SmallClassName,product_id,title,spec,buy_price,product_gain,price,unit,memo,updatetime,elite,passed,content,defaultpicurl) values('"&BigClassName&"','"&SmallClassName&"','"&product_id&"','"&title&"','"&spec&"','"&buy_price&"','"&product_gain&"','"&price&"','"&unit&"','"&memo&"','"&updatetime&"','"&elite&"','"&passed&"','"&content&"','"&goodsPic&"'"&")"
            'response.Write(sql)
            conn.execute(sql)
            response.Write "保存成功"
        end if
        set file=nothing
        next
    set upload=nothing
这里的话我们是通过引用了一个 上传类
那这个类的代码如下:
dim oUpFileStream

Class upload_file
 
dim Form,File,Version
 
Private Sub Class_Initialize
   '定义变量
  dim RequestBinDate,sStart,bCrLf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,oFileInfo
  dim iFileSize,sFilePath,sFileType,sFormvalue,sFileName
  dim iFindStart,iFindEnd
  dim iFormStart,iFormEnd,sFormName
   '代码开始
  Version="无组件上传类 Version 0.96"
  set Form = Server.CreateObject("Scripting.Dictionary")
  set File = Server.CreateObject("Scripting.Dictionary")
  if Request.TotalBytes < 1 then Exit Sub
  set tStream = Server.CreateObject("adodb.stream")
  set oUpFileStream = Server.CreateObject("adodb.stream")
  oUpFileStream.Type = 1
  oUpFileStream.Mode = 3
  oUpFileStream.Open
  oUpFileStream.Write Request.BinaryRead(Request.TotalBytes)
  oUpFileStream.Position=0
  RequestBinDate = oUpFileStream.Read
  iFormEnd = oUpFileStream.Size
  bCrLf = chrB(13) & chrB(10)
  '取得每个项目之间的分隔符
  sStart = MidB(RequestBinDate,1, InStrB(1,RequestBinDate,bCrLf)-1)
  iStart = LenB (sStart)
  iFormStart = iStart+2
  '分解项目
  Do
    iInfoEnd = InStrB(iFormStart,RequestBinDate,bCrLf & bCrLf)+3
    tStream.Type = 1
    tStream.Mode = 3
    tStream.Open
    oUpFileStream.Position = iFormStart
    oUpFileStream.CopyTo tStream,iInfoEnd-iFormStart
    tStream.Position = 0
    tStream.Type = 2
    tStream.Charset ="gb2312"
    sInfo = tStream.ReadText     
    '取得表单项目名称
    iFormStart = InStrB(iInfoEnd,RequestBinDate,sStart)-1
    iFindStart = InStr(22,sInfo,"name=""",1)+6
    iFindEnd = InStr(iFindStart,sInfo,"""",1)
    sFormName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
    '如果是文件
    if InStr (45,sInfo,"filename=""",1) > 0 then
      set oFileInfo= new FileInfo
      '取得文件属性
      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)
      oFileInfo.FileExt = GetFileExt(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 -2
      oFileInfo.FormName = sFormName
      file.add sFormName,oFileInfo
    else
    '如果是表单项目
      tStream.Close
      tStream.Type = 1
      tStream.Mode = 3
      tStream.Open
      oUpFileStream.Position = iInfoEnd
      oUpFileStream.CopyTo tStream,iFormStart-iInfoEnd-2
      tStream.Position = 0
      tStream.Type = 2
      tStream.Charset = "gb2312"
      sFormvalue = tStream.ReadText
      form.Add sFormName,sFormvalue
    end if
    tStream.Close
    iFormStart = iFormStart+iStart+2
    '如果到文件尾了就退出
    loop until (iFormStart+2) = iFormEnd
  RequestBinDate=""
  set tStream = nothing
End Sub

Private Sub Class_Terminate 
  '清除变量及对像
  if not Request.TotalBytes<1 then
    oUpFileStream.Close
    set oUpFileStream =nothing
    end if
  Form.RemoveAll
  File.RemoveAll
  set Form=nothing
  set File=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 GetFileExt(FullPath)
  If FullPath <> "" Then
    GetFileExt = mid(FullPath,InStrRev(FullPath, ".")+1)
    Else
    GetFileExt = ""
  End If
End function

End Class

'文件属性类
Class FileInfo
  dim FormName,FileName,FilePath,FileSize,FileType,FileStart,FileExt
  Private Sub Class_Initialize
    FileName = ""
    FilePath = ""
    FileSize = 0
    FileStart= 0
    FormName = ""
    FileType = ""
    FileExt = ""
  End Sub
 
'保存文件方法
 Public function SaveToFile(FullPath)
    dim oFileStream,ErrorChar,i
    SaveToFile=1
    if trim(fullpath)="" or right(fullpath,1)="/" then exit function
    set oFileStream=CreateObject("Adodb.Stream")
    oFileStream.Type=1
    oFileStream.Mode=3
    oFileStream.Open
    oUpFileStream.position=FileStart
    oUpFileStream.copyto oFileStream,FileSize
    oFileStream.SaveToFile FullPath,2
    oFileStream.Close
    set oFileStream=nothing
    SaveToFile=0
  end function
End Class
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载