一、服务端
asp的文件上传是采用二进制的方式,进行上传的。
<-- fileserver.jsp-->
<%
response.Charset="GB2312"
strConnectionString = session("data_str")
if strConnectionString<>"" then
Set cnn = server.CreateObject("ADODB.Connection")
cnn.Open strConnectionString
if err<>0 then
response.write "-99"
err.clear
response.end
end if
end if
if IsEmpty(session("userid")) or IsEmpty(Session("username")) then
isjttrue=false
ljzt="1"
response.write "-12"
err.clear
response.end
end if
'根据时间生成文件名称
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,"下午","")
Randomize
fname = int(fname) & int((1000-1+1)*Rnd + 1)
'ddi_sid=Request("jt_uniqueid") '集团合同
'rec_type=Request("rec_type") '类型
source_name=left(Request("source_file"), InstrRev(Request("source_file"), ".")-1) '原始文件名称
path=Request("path") '类型
Function getByteString(StringStr)
For i = 1 to Len(StringStr)
char = Mid(StringStr,i,1)
getByteString = getByteString & chrB(AscB(char))
Next
End Function
Function getFileExt(sFileName)
getFileExt = Mid(sFileName, InstrRev(sFileName, ".") + 1)
End Function
'Byte string to string conversion(hoho,this can deal with chinese!!!)
Function getString(str)
strto = ""
for i=1 to lenb(str)
if AscB(MidB(str, i, 1)) > 127 then
strto = strto & chr(Ascb(MidB(str, i, 1))*256+Ascb(MidB(str, i+1, 1)))
i = i + 1
else
strto = strto & Chr(AscB(MidB(str, i, 1)))
end if
next
getString=strto
End Function
Function getStringold(StringBin)
getString =""
For intCount = 1 to LenB(StringBin)
getString = getString & chr(AscB(MidB(StringBin,intCount,1)))
Next
End Function
PosBeg = 1
header_len=1 '报文头部的长度
boundary_end_size=1 '尾部分隔符长度
Dim UploadControl
Set UploadControl = CreateObject("Scripting.Dictionary")
Sub BuildUploadRequest(RequestBin) '接收端,剥离读取头部字段:
'Get the boundary
PosBeg = 1
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
boundary_end_size=PosEnd
boundary = MidB(RequestBin,PosBeg,PosEnd-PosBeg)
boundaryPos = InstrB(1,RequestBin,boundary)
'Get all data inside the boundaries 获取分隔符
b_pos=InstrB(RequestBin,boundary & getByteString("--"))
' Do until (boundaryPos=b_pos)
'Members variable of objects are put in a dictionary object
'Get an object name
Pos = InstrB(BoundaryPos,RequestBin,getByteString("Content-Disposition"))
Pos = InstrB(Pos,RequestBin,getByteString("name="))
PosBeg = Pos+6
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(34)))
Name = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
PosFile = InstrB(BoundaryPos,RequestBin,getByteString("filename="))
PosBound = InstrB(PosEnd,RequestBin,boundary)
'Test if object is of file type
If PosFile<>0 AND (PosFile<PosBound) Then
'Get Filename, content-type and content of file
PosBeg = PosFile + 10
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(34)))
FileName = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
'Add filename to dictionary object
UploadControl.Add "FileName", FileName
Pos = InstrB(PosEnd,RequestBin,getByteString("Content-Type:"))
PosBeg = Pos+14
PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
'Add content-type to dictionary object
ContentType = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
UploadControl.Add "ContentType",ContentType
'Get content of object
PosBeg = PosEnd+4
PosEnd = InstrB(PosBeg,RequestBin,boundary)-2
Value = MidB(RequestBin,PosBeg,PosEnd-PosBeg)
Else
'Get content of object
Pos = InstrB(Pos,RequestBin,getByteString(chr(13)))
PosBeg = Pos+4
PosEnd = InstrB(PosBeg,RequestBin,boundary)-2
Value = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
End If
Encoding="Content-Transfer-Encoding: binary"
stop
Content_Transfer_pos=InstrB(1,RequestBin,getByteString(Encoding))
'header_len=PosBeg+len(Encoding)-3
header_len=Content_Transfer_pos+len(Encoding)
'Add content to dictionary object
UploadControl.Add "Value" , Value
'Add dictionary object to main dictionary
'UploadRequest.Add name, UploadControl
'Loop to next object
BoundaryPos=InstrB(BoundaryPos+LenB(boundary),RequestBin,boundary)
' Loop
End Sub
on error resume next
Response.Buffer = TRUE
Response.Clear
filesize=Request.TotalBytes
RequestBin = Request.BinaryRead(filesize)
BuildUploadRequest RequestBin
Encoding="Content-Transfer-Encoding: binary"
Content_Transfer_pos=InstrB(1,RequestBin,getByteString(Encoding))
'header_len=PosBeg+len(Encoding)-3
header_len=Content_Transfer_pos+len(Encoding)+4 '回车和换行符
if err.number<>0 then
response.write "-1"
response.end
end if
FileName= UploadControl.Item("FileName") '获取上传文件的完整目录名字
attach_ext=getFileExt(FileName) '文件扩展名
compoundpic = UploadControl.Item("Value")
size=len(compoundpic) '文件字节大小
set str=server.CreateObject("ADODB.Stream")
str.Mode=3 '设置数据类型 3为可读可写
str.Type=1 '设置数据类型 1为二进制数据
str.Open
str.Write RequestBin
fname=fname&"."&attach_ext
filepath=path&"\"&source_name&"."&attach_ext 'Server.MapPath("../0/"&fname)
Set fs=Server.CreateObject("Scripting.FileSystemObject")
'if fs.FileExists(filepath)=false then
' set myfile=fs.CreateTextFile(filepath)
' myfile.close
'end if
if err.number<>0 then
response.write "-1"
response.end
end if
set desc=server.CreateObject("ADODB.Stream")
desc.Mode=3
desc.Type=1
desc.Open
str.position=header_len-1 '报文内容起始位置
'str.position=0 '报文内容起始位置
str.copyto desc,filesize-header_len-boundary_end_size-3 '拷贝字节
'str.copyto desc,filesize '拷贝字节
desc.SaveToFile filepath,2
desc.close
set desc=nothing
str.close
set str=nothing
set UploadControl=nothing
if err.number<>0 then
response.write "-1"
response.end
end if
if err.number<>0 then
response.write "-1"
response.end
end if
response.write 0
cnn.close
set cnn = nothing
%>