图片上传........ 解决马上结贴

119311canoe 2003-09-04 09:41:49
从服务器上添加的图片,图片路径(image_path)一定是在服务器端了,怎么能到客户端如何显示?
哪位能提供图片上传的代码?(服务器和客户断的都要有啊!)
小弟先谢谢大家了!
...全文
30 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lois80 2003-10-30
  • 打赏
  • 举报
回复
look
coolit 2003-09-09
  • 打赏
  • 举报
回复
有VB的上图片(文件)com的源代码吗?给我一份好吗?非常感谢!
coolit@163.com
萝卜波 2003-09-04
  • 打赏
  • 举报
回复
用组建上传吧,速度快很多多的
如果需要的话 carrot@reachgift 联系
vb源代码也有
tigerwen01 2003-09-04
  • 打赏
  • 举报
回复
ASP上传图片有两种方法,一种是有组件上传,另一种是无组件上传。
现给你一段无组件上传图片代码:
1)用户界面:getfile.htm
-------------------------
<html>

<head>
<title>保存图片到数据库</title>
</head>

<body>
<b>

<p></b>你可以找个图片试试,保存完毕后会有提示</p>

<form METHOD="POST" ENCTYPE="multipart/form-data" ACTION="savetodb.asp">
<p>Email : <input NAME="email" VALUE="wangcq@sina.com" size="20"><br/>
Picture : <input TYPE="file" NAME="blob"><br/>
<input TYPE="submit" NAME="Enter"> </p>
</form>
</body>
</html>

2)保存图片:savetodb.asp
----------------------------------
<%

Response.Buffer = TRUE
Response.Clear
byteCount = Request.TotalBytes

RequestBin = Request.BinaryRead(byteCount)
Dim UploadRequest
Set UploadRequest = CreateObject("Scripting.Dictionary")

BuildUploadRequest RequestBin

email = UploadRequest.Item("email").Item("Value")

contentType = UploadRequest.Item("blob").Item("ContentType")
filepathname = UploadRequest.Item("blob").Item("FileName")
filename = Right(filepathname,Len(filepathname)-InstrRev(filepathname,"\"))
picture = UploadRequest.Item("blob").Item("Value")

'Response.ContentType = contentType
'Response.binaryWrite picture

set objCn = server.createobject("adodb.connection")
set objRst = server.createobject("adodb.recordset")
objCn.Open "upload"
objrst.Open "pic", objcn, 1,3,2
objrst.addnew
objrst.fields("filename")=filename
objrst.fields("type")="gif"

objrst.fields("what").appendchunk picture
objrst.update
response.write "<a href=showpic.asp?id=" & objrst("id") & ">第" & objrst("id") & "个图片。</a>"
objrst.close

objCn.close
set objrst=nothing
set objcn = nothing
%>
<!--#include file="upload.asp"-->

3)显示图片文件:showpic.asp
----------------------------------------
<%
set objCn = server.createobject("adodb.connection")
set objRst = server.createobject("adodb.recordset")
objCn.Open "upload"
objrst.Open "select what from pic where id=" & request("id"), objcn

if not objrst.eof then
response.binarywrite objrst("what")
end if

objrst.close
objCn.close
set objrst=nothing
set objcn = nothing
%>


测试数据库:
test.mdb(dsn 名称:upload)
----------------------------------------
表pic:
id:自动加
filename:文本
type:文本
what:ole

-----------------------------------------
存成单个文件,放在一个目录下,打开(必须用http://...)getfile.htm
上传一个.gif或.jpg就可以显示了。
对于大文件在显示程序(showpic.asp)中可能会用到循环和getchunk方法。自己去做。记住,由于ASP目前暂时不支持二进行制读写,只能存二进制到数据库中。
有什么问题,请致电jackywen01@163.com
shushu2002 2003-09-04
  • 打赏
  • 举报
回复
<% set conn=server.createobject("adodb.connection")
set rs=server.createobject("adodb.recordset")
conn.open "Driver={Microsoft Access Driver (*.mdb)};dbq=" &server.mappath("数据库文件名.mdb")
%>
(以上内容为数据链接,最好单独写成一个.ASP文件,在其他文件需要的位置用“<!--#include file="该文件名.ASP"-->调用。另外,数据库文件最好用相对路径指明)



(以下内容为从本地磁盘浏览要上传图片)
<html>
<head>
<title>无标题文档</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<!----因为此上传图片是以更新字段的方式进行,所以产生一个随机数作为记录号---->
<% randomize() '使用此参数方可随机产生数字,否则只能0或1
k=cint(rnd()*10)
if k=0 then k=k+1 %>
<!----以下代码将在页面上产生一个“浏览...”按键,用于到本地磁盘选择要上传的图片---->
<form name="photoForm" enctype="multipart/form-data" action="sc1.asp?id1=<%=k%>" method="post">
<input type=file name="mefile" size="15">
<input type=submit name="ok" value="上传">
</form>
</body>
</html>



(以下内容执行上传)
<!--#include file="conn.asp"--> 
<!--“conn.asp”文件是事先建立好的数据链接文件,注意和自己建立的文件名匹配-->
<!---本页面执行上传操作,把图片写入数据库--->
<html>
<head>
<title>无标题文档</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<%
'因为图片是以更新的形式进行上传,所以首先检测指定记录号的记录是否存在
id1=request("id1") '取出上页传来的ID1值(用于确定操作的记录号)
session("id1")=id1 '把ID1值存入Session,下一页面将要使用
sql2="select * from tupian where id="&id1&""
set rs1=conn.execute(sql2)
if rs1.eof then '若记录不存在,则先建立它
sql1="insert into tupian(id) values("&id1&")"
conn.execute(sql1)
end if
rs1.close '关闭数据集
set rs1=nothing

'把图片进行必要的格式转换
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)

'找出指定记录
sql="select tp from tupian where id="&id1&""
rs.Open sql,conn,3,3

'执行图片字段更新,完成上传
rs("tp").appendchunk mydata
rs.update
rs.close
set rs=nothing
set conn=nothing
%>
上传成功,请看上传结果:
<meta http-equiv="refresh" content="1;url=xs.asp">
</body>
</html>



(以下内容执行显示被上传的图片)
<!--#include file="conn.asp"-->
<% '本页把上传的图片进行显示。注意:显示图片页不可有html语言标记,否则无法显示图片。
id1=session("id1") '取出上页传来的Session值
sql="select tp from tupian where id="&id1&" "
rs.Open sql,conn,1,3
response.contenttype="image/gif"
Response.BinaryWrite rs("tp")
session.abandon '清除Session值
%>

Good Luck! ^_^
chinesun 2003-09-04
  • 打赏
  • 举报
回复
saveupload.asp
___________________
<!--#include FILE="upload.asp"-->
<html>
<head>
<title>文件上传</title>
</head>
<body topmargin="0" leftmargin="0" bgcolor=#E4E4E4>
<table width="100%" border=0 cellspacing=0 cellpadding=0>
<tr><td valign=top height=40>
<%
Server.ScriptTimeOut=999999
'定义变量
dim Forumupload,ranNum
dim formName,formPath,filename,file_name,fileExt,Filesize,F_Type
dim rename,DownloadID

call upload_0()

'===========================无组件上传============================
sub upload_0()
dim upload,file
set upload=new UpFile_Class ''建立上传对象
'***************************************************
'***************此处写上不同的文件取值**************
'***************************************************

upload.GetDate (204800) '取得上传数据,204800=200K

if upload.err > 0 then
select case upload.err
case 1
Response.Write "请先选择你要上传的文件 [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
case 2
Response.Write "文件大小超过了200K的限制  [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
end select
exit sub
else
formPath=upload.form("filepath")
'在目录后加(/)
if right(formPath,1)<>"/" then formPath=formPath&"/"
for each formName in upload.file ''列出所有上传了的文件
set file=upload.file(formName) ''生成一个文件对象
fileExt=lcase(file.FileExt)

'判断文件类型
if lcase(fileEXT)="asp" and lcase(fileEXT)="asa" and lcase(fileEXT)="aspx" then
CheckFileExt(fileEXT)=false
end if
if CheckFileExt(fileEXT)=false then
response.write "文件格式不正确 [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
response.end
end if
'***************************************************
'***********此处不同的网站ranNum取值不同************
'***************************************************
'付值变量
if session("usid")="" then
response.write "非法用户!不能上传!"
response.end
else
ranNum=session("usid")
end if
ranNum=int(ranNum)+100000
ranNum=cstr(ranNum)&"_"
F_Type=CheckFiletype(fileEXT)
file_name=ranNum&year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)
filename=file_name&"."&fileExt
rename=filename&"|"
filename=formPath&filename
Filesize=file.FileSize

'记录文件
if Filesize>0 then '如果 FileSize > 0 说明有文件数据
file.SaveToFile Server.mappath(FileName) ''执行上传文件
call checksave() '记录文件
end if
set file=nothing
next
end if
set upload=nothing
response.write "文件上传成功![ <a href=# onclick=history.go(-1)>继续上传</a> ]"
end sub

Private sub checksave()
'插入上传表并获得ID
end sub

'判断文件类型是否合格
Private Function CheckFileExt (fileEXT)
dim Forumupload
Forumupload=split("gif|jpg|jpeg","|")
for i=0 to ubound(Forumupload)
if lcase(fileEXT)=lcase(trim(Forumupload(i))) then
CheckFileExt=true
exit Function
else
CheckFileExt=false
end if
next
End Function
%>
</td></tr>
</table>
</body>
</html>
chinesun 2003-09-04
  • 打赏
  • 举报
回复
saveupload.asp
___________________
<!--#include FILE="upload.asp"-->
<html>
<head>
<title>文件上传</title>
</head>
<body topmargin="0" leftmargin="0" bgcolor=#E4E4E4>
<table width="100%" border=0 cellspacing=0 cellpadding=0>
<tr><td valign=top height=40>
<%
Server.ScriptTimeOut=999999
'定义变量
dim Forumupload,ranNum
dim formName,formPath,filename,file_name,fileExt,Filesize,F_Type
dim rename,DownloadID

call upload_0()

'===========================无组件上传============================
sub upload_0()
dim upload,file
set upload=new UpFile_Class ''建立上传对象
'***************************************************
'***************此处写上不同的文件取值**************
'***************************************************

upload.GetDate (204800) '取得上传数据,204800=200K

if upload.err > 0 then
select case upload.err
case 1
Response.Write "请先选择你要上传的文件 [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
case 2
Response.Write "文件大小超过了200K的限制  [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
end select
exit sub
else
formPath=upload.form("filepath")
'在目录后加(/)
if right(formPath,1)<>"/" then formPath=formPath&"/"
for each formName in upload.file ''列出所有上传了的文件
set file=upload.file(formName) ''生成一个文件对象
fileExt=lcase(file.FileExt)

'判断文件类型
if lcase(fileEXT)="asp" and lcase(fileEXT)="asa" and lcase(fileEXT)="aspx" then
CheckFileExt(fileEXT)=false
end if
if CheckFileExt(fileEXT)=false then
response.write "文件格式不正确 [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
response.end
end if
'***************************************************
'***********此处不同的网站ranNum取值不同************
'***************************************************
'付值变量
if session("usid")="" then
response.write "非法用户!不能上传!"
response.end
else
ranNum=session("usid")
end if
ranNum=int(ranNum)+100000
ranNum=cstr(ranNum)&"_"
F_Type=CheckFiletype(fileEXT)
file_name=ranNum&year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)
filename=file_name&"."&fileExt
rename=filename&"|"
filename=formPath&filename
Filesize=file.FileSize

'记录文件
if Filesize>0 then '如果 FileSize > 0 说明有文件数据
file.SaveToFile Server.mappath(FileName) ''执行上传文件
call checksave() '记录文件
end if
set file=nothing
next
end if
set upload=nothing
response.write "文件上传成功![ <a href=# onclick=history.go(-1)>继续上传</a> ]"
end sub

Private sub checksave()
'插入上传表并获得ID
end sub

'判断文件类型是否合格
Private Function CheckFileExt (fileEXT)
dim Forumupload
Forumupload=split("gif|jpg|jpeg","|")
for i=0 to ubound(Forumupload)
if lcase(fileEXT)=lcase(trim(Forumupload(i))) then
CheckFileExt=true
exit Function
else
CheckFileExt=false
end if
next
End Function
%>
</td></tr>
</table>
</body>
</html>
chinesun 2003-09-04
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/TopicView1.asp?id=2154322
diya 2003-09-04
  • 打赏
  • 举报
回复
把地址改为相对路径并保存在相应的站点下就可以了,如src="../images/logo.gif",客户端通过访问这个站点是可以浏览到这图片的。

去下载个稻香老农制作无组件上传组件,里面有例子。
119311canoe 2003-09-04
  • 打赏
  • 举报
回复
谢谢大家的热情帮助,
再问一句:我要的是在服务器端,保存一本书的图片 到客户端显示,
我直接把图片路径保存的数据库表(book_image_path)中,到客户端在<%=book_image_path%>
这样做可以吗?为什么?

28,391

社区成员

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

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