Asp无组件上传源代码(改良版)

   'lngRequestSize 和binRequestData 都是上页的file控件直接提交过来的
在下一页直接接受传过来的数据就可以
 lngRequestSize = request.totalbytes
  binRequestData = request.binaryread(lngRequestSize)
  'session偵抣傪梌偊傞
  session("lngRequestSize") = lngRequestSize
  session("binRequestData") = binRequestData

Set objStream = Server.CreateObject("ADODB.Stream")
   lngRequestSize = session("lngRequestSize")
   binRequestData = session("binRequestData")  
   objStream.Type = 1
   objStream.Open
   objStream.Write binRequestData
    
   dim curr :  curr=1
   Set formStream = Server.CreateObject("ADODB.Stream")
    
   Dim bCrLf,strSeparator,intSeparator
   bCrLf=ChrB(13)&ChrB(10)            
   intSeparator=InstrB(1,binRequestData,bCrLf)-1
      
   strSeparator=LeftB(binRequestData,intSeparator)
   p_start = intSeparator+2
   p_end = intSeparator +2

   Dim p_start,p_end,intTemp
   dim looptime
   looptime =0
   dim headertext,disptext
   do while p_start+3    looptime = looptime+1
    p_start = p_end 
    p_end  =InStrB(p_start,binRequestData,bCrLf&bCrLf)+3
    formStream.Type=1
    formStream.Open
    formStream.Position=0
    objStream.Position=p_start
    objStream.CopyTo formStream,p_end -p_start
    formStream.Position=0
    formStream.Type=2
    formStream.Charset="SHIFT-JIS"
    headertext = formStream.ReadText     
    formStream.close()
     
    intTemp=Instr(39,headertext,"""")
    if Instr(intTemp,headertext,"filename=""")<>0 then 
     p_start=p_end
     p_end  =InStrB(p_start,binRequestData,strSeparator)-1
     objStream.Position=p_start
     formStream.Type=1
     formStream.open
     objStream.CopyTo formStream,p_end - p_start
     formStream.Position=0
     formStream.Type=2
     formStream.Charset="SHIFT-JIS"
     disptext = formStream.ReadText
     formStream.SaveToFile Pstr_Folder & Pstr_File_Name,2   
     b = split(disptext,vbcrlf)
     formStream.close
    else
     p_start=p_end
     p_end  =InStrB(p_start,binRequestData,strSeparator)-1
    end if
     p_start = p_end+intSeparator+2
    loop
    set formStream=nothing
    objStream.close
    set objStream=nothing

以上这段代码有时候上传后的文件会比实际的文件大出两个字节,因为涉及到最后一行读取换行的问题,但并不影响上传的文件的正确性,但是你感觉多出两个字节不爽的话,可以改进成下面这段代码就解决这个问题了

Set objStream = Server.CreateObject("ADODB.Stream")
   lngRequestSize = session("lngRequestSize")
   binRequestData = session("binRequestData") 
  
   objStream.Type = 1
   objStream.Open
   objStream.Write binRequestData
    
   dim curr :  curr=1
   Set formStream = Server.CreateObject("ADODB.Stream")
   
   Dim bCrLf,strSeparator,intSeparator
   bCrLf=ChrB(13)&ChrB(10)            
   intSeparator=InstrB(1,binRequestData,bCrLf)-1

   strSeparator=LeftB(binRequestData,intSeparator)
   p_start = intSeparator+2
   p_end = intSeparator +2

   p1_start = intSeparator+2
   p1_end = intSeparator +2

   looptime = 0
   do while p1_start+3    looptime=looptime+1
    p1_start = p1_end
    p1_end  =InStrB(p1_start,binRequestData,bCrLf&bCrLf)+3
    p1_start=p1_end
    p1_end  =InStrB(p1_start,binRequestData,strSeparator)-1
    p1_start = p1_end+intSeparator+2
   loop

   dim headertext,disptext,totaltext
   TempLoopTime = 0
   do while p_start+3    TempLoopTime = TempLoopTime + 1
    p_start = p_end 
    p_end  =InStrB(p_start,binRequestData,bCrLf&bCrLf)+3'終わりまで(へ)移動する   
    formStream.Type=1
    formStream.Open
    formStream.Position=0
    objStream.Position=p_start
    objStream.CopyTo formStream,p_end -p_start
    formStream.Position=0
    formStream.Type=2
    formStream.Charset="SHIFT-JIS"
    headertext = formStream.ReadText     'ヘッダーデータ取得する    
    formStream.close()     
    intTemp=Instr(39,headertext,"""") 

    if Instr(intTemp,headertext,"filename=""")<>0 then 'データ取得する
     p_start=p_end
     p_end  =InStrB(p_start,binRequestData,strSeparator)-1
     objStream.Position=p_start
     formStream.Type=1
     formStream.open    
     
     if TempLoopTime=(looptime-1) then
      objStream.CopyTo formStream,p_end - p_start-2
     else
      objStream.CopyTo formStream,p_end - p_start
     end if
     formStream.Position=0
     formStream.Type=2
     formStream.Charset="SHIFT-JIS"
     disptext = formStream.ReadText
     totaltext = totaltext & disptext
     formStream.SaveToFile Pstr_Folder & Pstr_File_Name &".Temp.bak" ,2   
     b = split(disptext,vbcrlf)
     formStream.close
    else     
     p_start=p_end
     p_end  =InStrB(p_start,binRequestData,strSeparator)-1
    end if
    p_start = p_end+intSeparator+2     
   loop
   set formStream=nothing
   objStream.close
   set objStream=nothing

你可能感兴趣的:(Asp)