浏览文件Show.asp
<%
Dim ID,TypeValue
ID=Request.QueryString("ID")
TypeValue=Request.QueryString("Type")
Session("DataCmd").CommandText="select * from MediaLib where ID="&CInt(ID)
Set Session("rs_MediaLib")=Server.CreateObject("ADODB.Recordset")
Session("rs_MediaLib").Open Session("DataCmd").CommandText,Session("DataConn"),1,3
Response.ContentType=TypeValue
Response.BinaryWrite Session("rs_MediaLib")("MediaFile")
%>
上载文件处理Result.asp
<%@ Language=VBScript %>
<script runat="server" language="vbscript">
Function GetUpload(FormData)
Dim DataStart,DivStr,DivLen,DataSize,FormFieldData
'分隔标志串(+CRLF)
DivStr = LeftB(FormData,InStrB(FormData,StrToBin(VbCrLf))+1)
'分隔标志串长度
DivLen=LenB(DivStr)
PosOpenBoundary=InStrB(FormData,DivStr)
PosCloseBoundary=InStrB(PosOpenBoundary + 1,FormData,DivStr)
Set Fields=CreateObject("Scripting.Dictionary")
While PosOpenBoundary>0 And PosCloseBoundary>0
'name起始位置(name="xxxxx"),加6是因为[name="]长度为6
FieldNameStart=InStrB(PosOpenBoundary,FormData,StrToBin("name="))+6
FieldNameSize=InStrB(FieldNameStart,FormData,ChrB(34))-FieldNameStart '(")的ASC值=34
FormFieldName=BinToStr(MidB(FormData,FieldNameStart,FieldNameSize))
'filename起始位置(filename="xxxxx")
FieldFileNameStart=InStrB(PosOpenBoundary,FormData,StrToBin("filename="))+10
If FieldFileNameStart<PosCloseBoundary And FieldFileNameStart>PosopenBoundary Then
FieldFileNameSize=InStrB(FieldFileNameStart,FormData,ChrB(34))-FieldFileNameStart '(")的ASC值=34
FormFileName=BinToStr(MidB(FormData,FieldFileNameStart,FieldFileNameSize))
Else
FormFileName=""
End If
'Content-Type起始位置(Content-Type: xxxxx)
FieldFileCTStart=InStrB(PosOpenBoundary,FormData,StrToBin("Content-Type:"))+14
If FieldFileCTStart<PosCloseBoundary And FieldFileCTStart>PosOpenBoundary Then
FieldFileCTSize=InStrB(FieldFileCTStart,FormData,StrToBin(VbCrLf&VbCrLf))-FieldFileCTStart
FormFileCT=BinToStr(MidB(FormData,FieldFileCTStart,FieldFileCTSize))
Else
FormFileCT=""
End If
'数据起始位置:2个CRLF开始
DataStart=InStrB(PosOpenBoundary,FormData,StrToBin(VbCrLf & VbCrLf))+4
If FormFileName<>"" Then
'数据长度,减1是因为数据文件的存取字节数问题(可能是AppendChunk方法的问题):
'由于字节数为奇数的图象存到数据库时会去掉最后一个字符导致图象不能正确显示,
'字节数为偶数的数据文件就不会出现这个问题,因此必须保持字节数为偶数。
DataSize=InStrB(DataStart,FormData,DivStr)-DataStart-1
FormFieldData=MidB(FormData,DataStart,DataSize)
Else
'数据长度,减2是因为分隔标志串前有一个CRLF
DataSize=InStrB(DataStart,FormData,DivStr)-DataStart-2
FormFieldData=BinToStr(MidB(FormData,DataStart,DataSize))
End If
'建立一个Dictionary集存储Form中各个Field的相关数据
Set Field=CreateUploadField()
Field.Name=FormFieldName
Field.FilePath=FormFileName
Field.FileName=GetFileName(FormFileName)
Field.ContentType=FormFileCT
Field.Length=LenB(FormFieldData)
Field.Value=FormFieldData
Fields.Add FormFieldName,Field
PosOpenBoundary=PosCloseBoundary
PosCloseBoundary=InStrB(PosOpenBoundary+1,FormData,DivStr)
Wend
Set GetUpload=Fields
End Function
Function BinToStr(binstr)
Dim varlen,clow,ccc,skipflag
'中文字符Skip标志
skipflag=0
ccc=""
If Not IsNull(binstr) Then
varlen=LenB(binstr)
For i=1 To varlen
If skipflag=0 Then
clow=MidB(binstr,i,1)
'判断是否中文的字符
If AscB(clow)>127 Then
'AscW会把二进制的中文双字节字符高位和低位反转,所以要先把中文的高低位反转
ccc=ccc&Chr(AscW(MidB(binstr,i+1,1)&clow))
skipflag=1
Else
ccc=ccc&Chr(AscB(clow))
End If
Else
skipflag=0
End If
Next
End If
BinToStr = ccc
End Function
'字符串->二进制字符
Function StrToBin(varstr)
StrToBin=""
For i=1 To Len(varstr)
varchar=mid(varstr,i,1)
varasc = Asc(varchar)
' asc对中文字符求出来的值可能为负数,
' 加上65536就可求出它的无符号数值
' -1在机器内是用补码表示的0xffff,
' 其无符号值为65535,65535=-1+65536
' 其他负数依次类推。
If varasc<0 Then
varasc = varasc + 65535
End If
'对中文的处理:把双字节低位和高位分开
If varasc>255 Then
varlow = Left(Hex(Asc(varchar)),2)
varhigh = right(Hex(Asc(varchar)),2)
StrToBin = StrToBin & chrB("&H" & varlow) & chrB("&H" & varhigh)
Else
StrToBin = StrToBin & chrB(AscB(varchar))
End If
Next
End Function
'取得文件名(去掉Path)
Function GetFileName(FullPath)
If FullPath <> "" Then
FullPath = StrReverse(FullPath)
FullPath = Left(FullPath, InStr(1, FullPath, "\") - 1)
GetFileName = StrReverse(FullPath)
Else
GetFileName = ""
End If
End Function
</script>
<script runat="server" language="jscript">
function CreateUploadField(){ return new uf_Init() }
function uf_Init(){
this.Name = null
this.FileName = null
this.FilePath = null
this.ContentType = null
this.Value = null
this.Length = null
}
</script>
<%
Dim FormData,FormSize
FormSize=Request.TotalBytes
FormData=Request.BinaryRead(FormSize)
Set Fields=GetUpload(FormData)
Set Session("rs_MediaLib")=Server.CreateObject("ADODB.Recordset")
Dim TypeValue,RecordId
TypeValue=Fields("TypeValue").Value
If TypeValue<>"" Then
Select Case TypeValue
Case "Modify"
RecordId=Fields("RecordId").Value
Session("rs_MediaLib").Open "select * from MediaLib where ID="&CInt(RecordId),Session("DataConn"),1,3
Session("rs_MediaLib")("Remark")=Fields("Remark"+RecordId).Value
Session("rs_MediaLib")("Category")=Fields("Category"+RecordId).Value
Session("rs_MediaLib")("Type")=Fields("Type"+RecordId).Value
If Fields("MediaFile"+RecordId).Value<>"" Then
Session("rs_MediaLib")("MediaFile").AppendChunk Fields("MediaFile"+RecordId).Value
End If
Session("rs_MediaLib").Update
Session("rs_MediaLib").Close
Set Session("rs_MediaLib")=Nothing
Response.Redirect "List.asp"
Case "Del"
RecordId=Fields("RecordId").Value
Session("DataCmd").CommandText="delete from MediaLib where ID="&CInt(RecordId)
Set Session("DataCmd").ActiveConnection=Session("DataConn")
Set Session("rs_main")=Session("DataCmd").Execute
Response.Redirect "List.asp"
Case "Add"
Session("rs_MediaLib").Open "select * from MediaLib",Session("DataConn"),1,3
Session("rs_MediaLib").AddNew
Session("rs_MediaLib")("Remark")=Fields("Remark").Value
Session("rs_MediaLib")("Category")=Fields("Category").Value
Session("rs_MediaLib")("Type")=Fields("Type").Value
If Fields("MediaFile").Value<>"" Then
Session("rs_MediaLib")("MediaFile").AppendChunk Fields("MediaFile").Value
End If
Session("rs_MediaLib").Update
Session("rs_MediaLib").Close
Set Session("rs_MediaLib")=Nothing
Response.Redirect "List.asp"
End Select
End If
%>
<html>