(100分赐教)ASP中如何保存图片路径到ACCESS并回显图片

romanmox 2009-04-01 09:23:30
本人刚刚接触ASP,还很菜,遇到如下问题请教各位大侠:
我想实现这样一个页面,通过点击“浏览”,指定本地的图片,并且可以通过文本框输入一些关于这幅图片的介绍信息和图片的路径一起保存到ACCESS中。
我在实现中遇到这样的问题:
不知道如何读取图片的路径,以及如何从数据库中读取路径进行图片的回显
我开始没有考虑保存图片的路径这种方法
而是在ACCESS中设置了一个OLE对象保存图片的二进制代码
图片是能保存成功了,可是随图片的介绍信息却不能保存成功,提示binaryread和Request.form不能同时使用之类的信息
请各位大侠不啬赐教
最好能贴出详细的代码供学习,100分奉上,诚望赐教!
...全文
996 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
niyewang 2009-12-28
  • 打赏
  • 举报
回复
<td width="100" height="100" rowspan="5">'长宽为100象素的表格
<img name="头像" src=" <%=(rs.Fields.Item("aaa").Value)%>" width="100" height="100" alt=""> </td>
为什么这段代码总是报错呢

Microsoft VBScript 编译器错误 (0x800A0400)
缺少语句
/Connections/imagelist2.asp, line 24
<td width=100 height=100 rowspan=5></td>
西安风影 2009-04-02
  • 打赏
  • 举报
回复
<html>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<body>
<!--#include FILE="upload_5xsoft.inc"-->
<%
set upload=new upload_5xsoft
response.write upload.form("userName")&"<br>"'获取form中的文本信息

set file=upload.file("fileImage")'获取form中提交的二进制信息
if file.fileSize>0 then
file.saveAs Server.mappath(file.FileName)
response.write "保存"&file.FileName&"文件成功"
end if
set file=nothing
set upload=nothing
%>
</body>
</html>
heyusuo 2009-04-02
  • 打赏
  • 举报
回复
差点忘了,调阅的时候很简单,我给你做个注释,如下:
<!--#include file="Connections/conn.asp" --> '连接数据库
<%
sql="select * from 用户信息表 order by id desc"
set rs=server.CreateObject("adodb.recordset")
rs.open sql,conn,1,1
%>
'以上是建立个记录集,"用户信息表"是你数据库中的表名称,就是你上传后保存图片路径的那张表
<td width="100" height="100" rowspan="5">'长宽为100象素的表格
<img name="头像" src="<%=(rs.Fields.Item("aaa").Value)%>" width="100" height="100" alt=""></td>
'以上两句就是显示的时候的代码,name="头像" 为图象占位符的名称,随便取 src="<%=(rs.Fields.Item("aaa").Value)% 这个最关键,是图象的链接路径, <%=(rs.Fields.Item("aaa").Value)%>意思是读取数据库"用户信息表"中的aaa字段,也就是上传后图片的路径及名称,这样就能显示了
有不懂的就问我,大家共同学习进步,呵呵
西安风影 2009-04-02
  • 打赏
  • 举报
回复
如果你要把图片当做二进制存入数据库,而且还要获取form其他文本
那么确实会存在
binaryread和Request.form不能同时存在的错误?
解决方法是 用无组件上传类。
index.htm
<html>
<body>
<form name="form1" method="post" action="upload.asp" enctype="multipart/form-data">
<input type="text" name="userName" /><br>
<input type="file" name="fileImage" /><br>
<input type="submit" name="submit" value="提交" />
</form>
</body>
</html>


upload.asp(单个文件)
<html>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<body>
<!--#include FILE="upload_5xsoft.inc"-->
<%
set upload=new upload_5xsoft
response.write upload.form("userName")&"<br>"

set file=upload.file("fileImage")
if file.fileSize>0 then
file.saveAs Server.mappath(file.FileName)
response.write "保存"&file.FileName&"文件成功"
end if
set file=nothing
set upload=nothing
%>
</body>
</html>

upload_5xsoft.inc:
<SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT>
dim Data_5xsoft

Class upload_5xsoft

dim objForm,objFile,Version

Public function Form(strForm)
strForm=lcase(strForm)
if not objForm.exists(strForm) then
Form=""
else
Form=objForm(strForm)
end if
end function

Public function File(strFile)
strFile=lcase(strFile)
if not objFile.exists(strFile) then
set File=new FileInfo
else
set File=objFile(strFile)
end if
end function


Private Sub Class_Initialize
dim RequestData,sStart,vbCrlf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,theFile
dim iFileSize,sFilePath,sFileType,sFormValue,sFileName
dim iFindStart,iFindEnd
dim iFormStart,iFormEnd,sFormName
Version="化境HTTP上传程序 Version 2.1"
set objForm=Server.CreateObject("Scripting.Dictionary")
set objFile=Server.CreateObject("Scripting.Dictionary")
if Request.TotalBytes<1 then Exit Sub
set tStream = Server.CreateObject("adodb.stream")
set Data_5xsoft = Server.CreateObject("adodb.stream")
Data_5xsoft.Type = 1
Data_5xsoft.Mode =3
Data_5xsoft.Open
Data_5xsoft.Write Request.BinaryRead(Request.TotalBytes)
Data_5xsoft.Position=0
RequestData =Data_5xsoft.Read

iFormStart = 1
iFormEnd = LenB(RequestData)
vbCrlf = chrB(13) & chrB(10)
sStart = MidB(RequestData,1, InStrB(iFormStart,RequestData,vbCrlf)-1)
iStart = LenB (sStart)
iFormStart=iFormStart+iStart+1
while (iFormStart + 10) < iFormEnd
iInfoEnd = InStrB(iFormStart,RequestData,vbCrlf & vbCrlf)+3
tStream.Type = 1
tStream.Mode =3
tStream.Open
Data_5xsoft.Position = iFormStart
Data_5xsoft.CopyTo tStream,iInfoEnd-iFormStart
tStream.Position = 0
tStream.Type = 2
tStream.Charset ="gb2312"
sInfo = tStream.ReadText
tStream.Close
'取得表单项目名称
iFormStart = InStrB(iInfoEnd,RequestData,sStart)
iFindStart = InStr(22,sInfo,"name=""",1)+6
iFindEnd = InStr(iFindStart,sInfo,"""",1)
sFormName = lcase(Mid (sinfo,iFindStart,iFindEnd-iFindStart))
'如果是文件
if InStr (45,sInfo,"filename=""",1) > 0 then
set theFile=new FileInfo
'取得文件名
iFindStart = InStr(iFindEnd,sInfo,"filename=""",1)+10
iFindEnd = InStr(iFindStart,sInfo,"""",1)
sFileName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
theFile.FileName=getFileName(sFileName)
theFile.FilePath=getFilePath(sFileName)
theFile.FileExt=GetFileExt(sFileName)
'取得文件类型
iFindStart = InStr(iFindEnd,sInfo,"Content-Type: ",1)+14
iFindEnd = InStr(iFindStart,sInfo,vbCr)
theFile.FileType =Mid (sinfo,iFindStart,iFindEnd-iFindStart)
theFile.FileStart =iInfoEnd
theFile.FileSize = iFormStart -iInfoEnd -3
theFile.FormName=sFormName
if not objFile.Exists(sFormName) then
objFile.add sFormName,theFile
end if
else
'如果是表单项目
tStream.Type =1
tStream.Mode =3
tStream.Open
Data_5xsoft.Position = iInfoEnd
Data_5xsoft.CopyTo tStream,iFormStart-iInfoEnd-3
tStream.Position = 0
tStream.Type = 2
tStream.Charset ="gb2312"
sFormValue = tStream.ReadText
tStream.Close
if objForm.Exists(sFormName) then
objForm(sFormName)=objForm(sFormName)&", "&sFormValue
else
objForm.Add sFormName,sFormValue
end if
end if
iFormStart=iFormStart+iStart+1
wend
RequestData=""
set tStream =nothing
End Sub

Private Sub Class_Terminate
if Request.TotalBytes>0 then
objForm.RemoveAll
objFile.RemoveAll
set objForm=nothing
set objFile=nothing
Data_5xsoft.Close
set Data_5xsoft =nothing
end if
End Sub


Private function GetFilePath(FullPath)
If FullPath <> "" Then
GetFilePath = left(FullPath,InStrRev(FullPath, "\"))
Else
GetFilePath = ""
End If
End function

Private function GetFileExt(FullPath)
If FullPath <> "" Then
GetFileExt = mid(FullPath,InStrRev(FullPath, ".")+1)
Else
GetFileExt = ""
End If
End function

Private function GetFileName(FullPath)
If FullPath <> "" Then
GetFileName = mid(FullPath,InStrRev(FullPath, "\")+1)
Else
GetFileName = ""
End If
End function
End Class

Class FileInfo
dim FormName,FileName,FilePath,FileSize,FileExt,FileType,FileStart
Private Sub Class_Initialize
FileName = ""
FilePath = ""
FileSize = 0
FileStart= 0
FormName = ""
FileType = ""
FileExt = ""
End Sub

Public function SaveAs(FullPath)
dim dr,ErrorChar,i
SaveAs=true
if trim(fullpath)="" or FileStart=0 or FileName="" or right(fullpath,1)="/" then exit function
set dr=CreateObject("Adodb.Stream")
dr.Mode=3
dr.Type=1
dr.Open
Data_5xsoft.position=FileStart
Data_5xsoft.copyto dr,FileSize
dr.SaveToFile FullPath,2
dr.Close
set dr=nothing
SaveAs=false
end function
End Class
</SCRIPT>
heyusuo 2009-04-02
  • 打赏
  • 举报
回复
接楼上,upload_5xsoft.inc代码如下:
<SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT>
dim Data_5xsoft

Class upload_5xsoft

dim objForm,objFile,Version

Public function Form(strForm)
strForm=lcase(strForm)
if not objForm.exists(strForm) then
Form=""
else
Form=objForm(strForm)
end if
end function

Public function File(strFile)
strFile=lcase(strFile)
if not objFile.exists(strFile) then
set File=new FileInfo
else
set File=objFile(strFile)
end if
end function


Private Sub Class_Initialize
dim RequestData,sStart,vbCrlf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,theFile
dim iFileSize,sFilePath,sFileType,sFormValue,sFileName
dim iFindStart,iFindEnd
dim iFormStart,iFormEnd,sFormName
Version="化境HTTP上传程序 Version 2.0"
set objForm=Server.CreateObject("Scripting.Dictionary")
set objFile=Server.CreateObject("Scripting.Dictionary")
if Request.TotalBytes<1 then Exit Sub
set tStream = Server.CreateObject("adodb.stream")
set Data_5xsoft = Server.CreateObject("adodb.stream")
Data_5xsoft.Type = 1
Data_5xsoft.Mode =3
Data_5xsoft.Open
Data_5xsoft.Write Request.BinaryRead(Request.TotalBytes)
Data_5xsoft.Position=0
RequestData =Data_5xsoft.Read

iFormStart = 1
iFormEnd = LenB(RequestData)
vbCrlf = chrB(13) & chrB(10)
sStart = MidB(RequestData,1, InStrB(iFormStart,RequestData,vbCrlf)-1)
iStart = LenB (sStart)
iFormStart=iFormStart+iStart+1
while (iFormStart + 10) < iFormEnd
iInfoEnd = InStrB(iFormStart,RequestData,vbCrlf & vbCrlf)+3
tStream.Type = 1
tStream.Mode =3
tStream.Open
Data_5xsoft.Position = iFormStart
Data_5xsoft.CopyTo tStream,iInfoEnd-iFormStart
tStream.Position = 0
tStream.Type = 2
tStream.Charset ="gb2312"
sInfo = tStream.ReadText
tStream.Close
'取得表单项目名称
iFormStart = InStrB(iInfoEnd,RequestData,sStart)
iFindStart = InStr(22,sInfo,"name=""",1)+6
iFindEnd = InStr(iFindStart,sInfo,"""",1)
sFormName = lcase(Mid (sinfo,iFindStart,iFindEnd-iFindStart))
'如果是文件
if InStr (45,sInfo,"filename=""",1) > 0 then
set theFile=new FileInfo
'取得文件名
iFindStart = InStr(iFindEnd,sInfo,"filename=""",1)+10
iFindEnd = InStr(iFindStart,sInfo,"""",1)
sFileName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
theFile.FileName=getFileName(sFileName)
theFile.FilePath=getFilePath(sFileName)
'取得文件类型
iFindStart = InStr(iFindEnd,sInfo,"Content-Type: ",1)+14
iFindEnd = InStr(iFindStart,sInfo,vbCr)
theFile.FileType =Mid (sinfo,iFindStart,iFindEnd-iFindStart)
theFile.FileStart =iInfoEnd
theFile.FileSize = iFormStart -iInfoEnd -3
theFile.FormName=sFormName
if not objFile.Exists(sFormName) then
objFile.add sFormName,theFile
end if
else
'如果是表单项目
tStream.Type =1
tStream.Mode =3
tStream.Open
Data_5xsoft.Position = iInfoEnd
Data_5xsoft.CopyTo tStream,iFormStart-iInfoEnd-3
tStream.Position = 0
tStream.Type = 2
tStream.Charset ="gb2312"
sFormValue = tStream.ReadText
tStream.Close
if objForm.Exists(sFormName) then
objForm(sFormName)=objForm(sFormName)&", "&sFormValue
else
objForm.Add sFormName,sFormValue
end if
end if
iFormStart=iFormStart+iStart+1
wend
RequestData=""
set tStream =nothing
End Sub

Private Sub Class_Terminate
if Request.TotalBytes>0 then
objForm.RemoveAll
objFile.RemoveAll
set objForm=nothing
set objFile=nothing
Data_5xsoft.Close
set Data_5xsoft =nothing
end if
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
End Class

Class FileInfo
dim FormName,FileName,FilePath,FileSize,FileType,FileStart
Private Sub Class_Initialize
FileName = ""
FilePath = ""
FileSize = 0
FileStart= 0
FormName = ""
FileType = ""
End Sub

Public function SaveAs(FullPath)
dim dr,ErrorChar,i
SaveAs=true
if trim(fullpath)="" or FileStart=0 or FileName="" or right(fullpath,1)="/" then exit function
set dr=CreateObject("Adodb.Stream")
dr.Mode=3
dr.Type=1
dr.Open
Data_5xsoft.position=FileStart
Data_5xsoft.copyto dr,FileSize
dr.SaveToFile FullPath,2
dr.Close
set dr=nothing
SaveAs=false
end function
End Class
</SCRIPT>
heyusuo 2009-04-02
  • 打赏
  • 举报
回复
我也是菜鸟,但我刚解决了这个问题,我可以给你全部代码,用的是ASP无组件上传,代码如下:
upfile.asp
<html>
<head>
<title>文件上传</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">

td { font-size: 9pt}
a { color: #000000; text-decoration: none}
a:hover { text-decoration: underline}
.tx { height: 16px; width: 30px; border-color: black black #000000; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 1px; border-left-width: 0px; font-size: 9pt; background-color: #eeeeee; color: #0000FF}
.tx1 { height: 20px; width: 30px; font-size: 9pt; border: 1px solid; border-color: black black #000000; color: #0000FF}
-->
</style>
</head>

<body>
<form name="form1" method="post" action="upload.asp?txtid=<%=request.QueryString("txtid")%>" enctype="multipart/form-data" >
<input type="hidden" name="act" value="upload">
<table width="40%" border="0" cellspacing="0" cellpadding="0" align="left">


<tr align="center" valign="middle">
<td align="left" id="upid" height="18">
<input type="file" name="file1" style="width:204" class="tx1" value="">
<label></label></td>
</tr>
</table>
</form>
</body>
</html>

uplode.asp
<!--#include file="connection.inc" -->'连接数据库语句

<%'OPTION EXPLICIT%>
<%Server.ScriptTimeOut=5000%>

<html>
<head>
<title>文件上传</title>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<meta http-equiv="Content-Language" Content="zh-CN">

</head>

<body leftmargin="10" topmargin="10" bgcolor="#FFFFFF">

<!-- #include file="upload_5xsoft.inc" -->

<%
dim upload,file,formName,iCount,FolderNameNew
dim GuFolderPath,fso,GuFolder0,GuFileSize0,GuFileExt0,GuAutoName

set upload=new upload_5xsoft '建立上传对象

GuFolder0="aaa/" '设定默认上传的目录,必须以“/”结束,可以为空
GuFileSize0=2048 '设定默认允许上传的最大文件,单位:K,1024K=1M
GuFileExt0="bmp|gif|jpg|jpeg|png|swf|rar|zip|txt" '设定默认允许上传的文件类型
GuAutoName="1" '设定上传成功后的文件名是否自动重新命名或是使用原来的名称,1为是,0为否

Response.write upload.Version&"<br><br>" '显示上传类的版本


if upload.form("GuFolderPath")<>"" then
GuFolderPath=upload.form("GuFolderPath")
call FolderNameCheck(GuFolderPath)
GuFolderPath=upload.form("GuFolderPath")
if right(GuFolderPath,1)<>"/" then GuFolderPath=GuFolderPath&"/"

elseif upload.form("GuFolderPath")="" and GuFolder0<>"" then
GuFolderPath=GuFolder0
call FolderNameCheck(GuFolderPath)
GuFolderPath=GuFolder0
if right(GuFolderPath,1)<>"/" then GuFolderPath=GuFolderPath&"/"

else
GuFolderPath=""

end if


iCount=0
for each formName in upload.objForm '列出所有form数据
Response.write formName&"="&upload.form(formName)&"<br>"
next

Response.write "<br><br>"


for each formName in upload.objFile '列出所有上传了的文件

set file=upload.file(formName)

if file.FileSize>0 then

dim FileExtF,FileExtY,FileExtOK,ii,jj
FileExtF=split(File.FileName,".")
for jj=0 to ubound(FileExtF)
next
FileExtY=0
FileExtOK=split(GuFileExt0,"|")

for ii=0 to ubound(FileExtOK)
if FileExtOK(ii)=FileExtF(jj-1) then
FileExtY=1
exit for
end if
next

if FileExtY=0 then
Htmend "上传失败,不允许上传的文件类型"

elseif file.FileSize>GuFileSize0*1024 then
Htmend "上传失败,单个文件大小超过限制,最大"&GuFileSize0&"*1024 字节,1K=1024字节"

else
dim FileNameOK
if GuAutoName="1" then
FileNameOK=year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&iCount&"."&FileExtF(jj-1)
else
FileNameOK=file.FileName
end if

file.SaveAs Server.mappath(GuFolderPath&FileNameOK) '保存文件
Response.write file.FilePath&file.FileName&"(大小:"&file.FileSize&" 字节) => "&GuFolderPath&FileNameOK&" 成功! <br>"
iCount=iCount+1
'写入数据库操作开始
strSQL = "insert into test(a) values('" &GuFolderPath&FileNameOK& "')"
' strSQL = "insert into test(a,b) values('" & FileNameOK & "','" & file.FileSize & "')"
'插入数据到数据表中的某字段
'写入数据库结束
'测试SQL语句开始
'response.Write strSQL
'Response.End()
'测试SQL语句结束
dbconnection.Execute strSQL
end if

else
Htmend "上传失败,请选择要上传的文件"

end if

set file=nothing

next

set upload=nothing

Htmend iCount&" 个文件上传成功!"


Sub FolderNameCheck(FolderNameNew)

dim Letters,i,c
Letters="+=:;,[]<>\|*?"
for i=1 to len(FolderNameNew)
c=mid(FolderNameNew,i,1)
if inStr(Letters,c)<>0 then
Htmend "上传失败,文件夹名称含有特殊字符"
end if
next

GuFolderPath=server.MapPath(GuFolderPath)
Set fso=Server.CreateObject("Scripting.FileSystemObject")
if fso.FolderExists(GuFolderPath)=false then
fso.CreateFolder(GuFolderPath)
end if
Set fso=nothing
End sub
Sub HtmEnd(Msg)
set upload=nothing
response.write "<br>"&Msg&" <br><br><input type=""button"" value="" 返 回 "" onclick=""javascript:history.back();""></body></html>"
response.end
End sub
%>
</body>
</html>

还有个upload_5xsoft.inc我在后边补上,我都做注释了,有不懂的联系我,抢分,哈哈
romanmox 2009-04-02
  • 打赏
  • 举报
回复
急啊,还有达人们能提供点帮助的么

我在网上找了好多无组件上传的代码,但是好多都是把图片以二进制形式保存进数据库的……
zhwaterlong 2009-04-02
  • 打赏
  • 举报
回复
本地上传是一样的道理,建议你去下个无组件上传的代码,最好不要采取直接把图片以二进制存进数据库,读取文件名,然后保存到指定文件夹
romanmox 2009-04-02
  • 打赏
  • 举报
回复
结贴了,问题也解决了,多谢heyusuo 和changke18 的帮助,以后多多指教!衷心感谢!
romanmox 2009-04-02
  • 打赏
  • 举报
回复
图片在本机上,只要读取当前图片的路径保存进数据库就可以了啊,不是远程上传图片
牟海川 2009-04-02
  • 打赏
  • 举报
回复
你下载一个上传图片的源代码来研究吧
看你的要求,图片要上传到一个文件夹,然后把这个路径读出来,写到数据库就可以了
romanmox 2009-04-02
  • 打赏
  • 举报
回复
有能解决这个问题的达人么?

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧