为什么我上传文件到数据库后,在数据哭里看到的都是?呢,还有在数据库中文件名不全。
大家看看我的代码
<!--#include file="ContentStr.asp"-->
<%
'函数功能,将二进制字符串转换成文本字符
function cByteString(bString)
dim index
for index=1 to LenB(bString) 'LenB上计算字节数,一个字符有2个字节表示。
bChar=midB(bString,index,1) ' bString中的取一个 字节
if(ascB(bChar)>127) then
CByteString=CByteString&chr(ascW(midB(bString,index+1,1)&bChar))
index=index+1
else
CBytString=CByteString&chr(ascB(midB(bString,index,1)))
end if
next
end function
'读取表单数据(二进制)
length=request.TotalBytes
bFileInfo=request.BinaryRead(length)
response.Write(length&"<br>")
response.Write(bFileInfo)
'读取数据后分析数据,定义CRLF标志的二进制字符串
bCRLF=chrB(13)&chrB(10)
'截取边界的二进制字符串
dividerLen=instrB(bFileInfo,bCRLF)-1 '参考instr函数
bDivider=leftB(bFileInfo,dividerLen)
'截取头部信息,并转换为文本字符串
headerLen=instrB(bFileInfo,bCRLF&bCRLF)-1
bHeader=leftB(bFileInfo,headerLen)
sHeader=cByteString(bHeader)
'提取文件名
nameStartPos=instr(sHeader,"filename="&chr(34))+len("filename="&chr(34))
nameEndPos=instr(nameStartPos,sHeader,chr(34))
fileName=mid(sHeader,nameStartPos,(nameStartPos-nameEndPos)) '取字符串中的几个字
'提取文件内容类型
typeStartPos=instr(sHeader,"Content-Type:")+len("Content-Type/:")'返回某字符串在另一字符串中第一次出现的位置
contentType=mid(sHeader,typeStartPos)
'获取文件内容(二进制形式)
contentStartPos=instrB(bFileInfo,bCRLF&bCRLF)+4
contentEndPos=instrB(contentStartPos,bFileInfo,bDivider)
bFileContent=midB(bFileInfo,contentStartPos,(contentEndPos-contentStartPos))
'将文件信息写入数据库
SqlStr="select * from UpFile"
Set Res=Server.CreateObject("ADODB.RecordSet")
Res.open SqlStr,Conn,3,2
Res.addnew
Res("FileName")=fileName
Res("FileClass")=contentType
Res("FileContent").appendChunk bFileContent
Res.update
Res.close
Set Res=nothing
Conn.close
Set Conn=nothing
Response.Write("文件:"&fileName&"成功上传到数据库!")
%>