高分紧急求救!ASP如何用无组件方法上传图片及信息至数据库?

mseliqin 2004-12-06 02:29:40
小弟的想法是将图片及信息一次提交上传至access数据库的一个表中,以后在页面中通过超连接,传递信息参数在数据库中找到图片,并在页面中显示出来.最好提供源码或相关的文章.谢了.
...全文
187 点赞 收藏 19
写回复
19 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
sifn 2004-12-10
<%
声明:此上传类是在化境编程界发布的无组件上传类的基础上修改的.
'在与化境编程界无组件上传类相比,速度快了将近50倍,当上传4M大小的文件时
'服务器只需要10秒就可以处理完,是目前最快的无组件上传程序,当前版本为0.96
'源代码公开,免费使用,对于商业用途,请与作者联系
'文件属性:例如上传文件为c:\myfile\doc.txt
'FileName 文件名 字符串 "doc.txt"
'FileSize 文件大小 数值 1210
'FileType 文件类型 字符串 "text/plain"
'FileExt 文件扩展名 字符串 "txt"
'FilePath 文件原路径 字符串 "c:\myfile"
'使用时注意事项:
'由于Scripting.Dictionary区分大小写,所以在网页及ASP页的项目名都要相同的大小
'写,如果人习惯用大写或小写,为了防止出错的话,可以把
'sFormName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
'改为
'(小写者)sFormName = LCase(Mid (sinfo,iFindStart,iFindEnd-iFindStart))
'(大写者)sFormName = UCase(Mid (sinfo,iFindStart,iFindEnd-iFindStart))
'**********************************************************************
'----------------------------------------------------------------------
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
%>
QQ19440242
回复
ruyunluck 2004-12-10
我刚开始也是用这个方法,走了许多弯路,建议不要用这个方法。把图片地址保存到数据库中。当然如果用于邮件发送还是可以。这是我的一点体会。
回复
liuyangxuan 2004-12-10
为什么天天有人总是问个问题?
回复
tongrong515 2004-12-10
请你加我的QQ咯!我发给你咯!286627998
回复
gu1dai 2004-12-10

回复
yb2008 2004-12-10
尽量不要把图片写到数据库中,传到指定的文件夹就可以了,把图片名称写入到数据库中!

http://www.hc189.com/upload.rar
回复
tigerhu76 2004-12-10
mark
回复
明珠佩佩 2004-12-10
往SQL数据库上传的完整代码,连接部分我就不写了

'upload.asp
<html>
<body>
<center>   
<form name="mainForm" enctype="multipart/form-data" action="process.asp" method=post>     
<input type=file name=mefile><br>   
<input type=submit name=ok value="OK">   
</form>
</center>
</body>
</html>

'process.asp
<%
response.buffer=true
formsize=request.totalbytes
formdata=request.binaryread(formsize)
bncrlf=chrB(13) & chrB(10)
divider=leftB(formdata,clng(instrb(formdata,bncrlf))-1)
datastart=instrb(formdata,bncrlf & bncrlf)+4
dataend=instrb(datastart+1,formdata,divider)-datastart
mydata=midb(formdata,datastart,dataend)

set connGraph=Server.CreateObject("ADODB.connection")
connGraph.ConnectionString = "driver={SQL Server};server=PIONEER-BIMY;uid=sa;pwd=20040808;database=VSOA"
connGraph.Open

set rec=server.createobject("ADODB.recordset")
rec.Open "SELECT * FROM [images] where id is null",connGraph,1,3
rec.addnew
rec("img").appendchunk mydata
rec.update
rec.close

set rec=nothing
set connGraph=nothing
%>

'show.asp
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>New Page 1</title>
</head>

<body>
<IMG SRC="showing.asp?id=5">
</body>

</html>

'showing.asp
<%
set connGraph=Server.CreateObject("ADODB.connection")
connGraph.ConnectionString = "driver={SQL Server};server=PIONEER-BIMY;uid=sa;pwd=20040808;database=VSOA"
connGraph.Open

set rec=server.createobject("ADODB.recordset")
strsql="select img from images where id= "& trim(request("id"))
rec.open strsql,connGraph,1,1
Response.ContentType = "image/*"
Response.BinaryWrite rec("img").getChunk(7500000)
rec.close

set rec=nothing
set connGraph=nothing
%>


回复
chunling2 2004-12-10
我也收藏一下
回复
每个图片一个response.clear
回复
mseliqin 2004-12-09
自己顶一下,这几天太郁闷了.在楼上 fangq(那一夜,你让我失眠!!)大虾的帮助下,上传成功,单显示却出了问题,我的显示是这样写的:由两个页面组成,第一个页面根据其他关系得到图片的id号,然后用
<img src="xxx.asp?id=xxx"> 传道另一页面读出access中的数据,显示在第一个页面,但是无论id号是多少,都显示数据库中的第一条记录,如果删除第一条记录,图片就不显示了.源码就不写了,就是楼上提供的源码.请问,各位大侠,这是什么地方有错,块块帮帮我吧.
回复
huamulan 2004-12-06
网上有很多源码的,如化境无组件上传、无惧无组件上传、先锋无组件上传

搜索一下,均提供上传到数据库的示例源码
回复
fangq 2004-12-06
源码如下:

-------------------------写入access-----------
<%
response.buffer=true
formsize=request.totalbytes
formdata=request.binaryread(formsize)
bncrlf=chrB(13) & chrB(10)
divider=leftB(formdata,clng(instrb(formdata,bncrlf))-1)
datastart=instrb(formdata,bncrlf & bncrlf)+4
dataend=instrb(datastart+1,formdata,divider)-datastart
mydata=midb(formdata,datastart,dataend)
set conn=Server.CreateObject("ADODB.Connection")
conn.open "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("fangqing.mdb")
set rec=server.createobject("ADODB.recordset")
rec.Open "SELECT * from [fangqing] where id is null",conn,1,3
rec.addnew
rec("img").appendchunk mydata
rec.update
rec.close
set rec=nothing
set connGraph=nothing
%>

------------------------------读access---------------------
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.open "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("fangqing.mdb")
set rs = server.CreateObject("ADODB.Recordset")
sql="select * from fangqing where id="&request("id")
rs.open sql,conn,1,3
Response.ContentType = "image/*"
Response.BinaryWrite rs("img").getChunk(7500000)
rs.close
set rs=nothing
set conn=nothing
%>

--------------------------显示images----------------

<img src="xs.asp?id=10">
回复
welunzhu 2004-12-06
不要把圖片寫到數據庫中去,這樣很損庫的

你只要把連接地址寫到庫中就可以阿


圖片還是專門上傳到一個(upfiles )中就好啊
回复
yqh1314 2004-12-06
很不错呢!
回复
http://blog.csdn.net/liuxiaoyi666
回复
Clove 2004-12-06
搞个File类型的,使用二进制写入数据库就好了
不过别忘了记录文件名,文件大小类型
回复
iuhxq 2004-12-06
这个太多了啊,看看偶这个:http://blog.csdn.net/iuhxq/archive/2004/06/03/20194.aspx

把里面的上传提取出来就行了,很方便的!
回复
ckxp 2004-12-06
http://www0.ccidnet.com/tech/web/2000/04/26/58_632.html
http://www.kupage.com/webdesign/7/20031017/1651460000027pkxd20u.htm
回复
相关推荐
发帖
ASP
创建于2007-09-28

2.8w+

社区成员

ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
申请成为版主
帖子事件
创建了帖子
2004-12-06 02:29
社区公告
暂无公告