图片如何上传

沙沙的吹 2003-08-23 06:07:33
我要做一个程序
把表单数据写到数据库
可是图片数据如何上传我大懂
我在一本资料看到一个如何上传图片的问题
试验并成功了
可是那个程序只有一个输入框(即输入文件名的框),所以接受断就直接提取数据
可是我需要的程序有好几个输入框,就是说传给ASP文件的不只是图片文件数据
我如何把其它数据提取出来
哪里有这方面的资料,特别是request读取回来的信息的组织方式的资料。我的看到的测试程序是这样的:
testupload.htm ''测试upload.asp的文件
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>

<body>
<form action="upload.asp" method="post" enctype="multipart/form-data" name="form1">
<p>  </p>
<p>
<input type="File" name="ImageFile">
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>
</html>

下面是upload.asp
<!--#include file="inc/conn.asp"-->
<%
response.Write(request("ID"))
lngCount=Request.TotalBytes
postedData=Request.BinaryRead(lngCount)
crlf=ChrB(13)&ChrB(10)
tmpData=LeftB(postedData,Clng(InStrB(PostedData,crlf)))
dataStart=InstrB(postedData,Crlf&crlf)+4
dataEnd=InStrB(dataStart+1,postedData,tmpData)-dataStart
starData=MidB(PostedData,dataStart,dataEnd)


rs.CursorType=adOpenKeyset
rs.LockType=3
rs.Open "News_Pic",conn,,,adCmdTable
rs.AddNew

rs("News_ID")=1
rs("Pic_Data").AppendChunk starData
rs.UpDate
rs.close

%>
...全文
99 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
全息宇宙 2003-08-24
  • 打赏
  • 举报
回复
给你个例子吧:

upload.asp:

<table width="26%" border="0" height="15" cellspacing="0" cellpadding="0">
<form name="form" method="post" action="upfile.asp?info=asfsdfs" enctype="multipart/form-data">

<tr>
<td colspan="2">
<input type="text" name="info" size="30">
<font size="2" color="#CC0000"><b>图片标题</b></font>
</td>
</tr>

<tr>
<td width="28%">
<input type="file" name="file1" size=22>
</td>
<td width="72%"> 
<input type="submit" name="Submit" value="上传">
</td>
</tr>
<tr>
<td colspan="2"><font color="#666666" size="2">建议图片:宽200象素,高度不限,图片不超过10副.</font></td>
</tr>
</form>
</table>



upfile.asp:


<%

dim upfile_5xSoft_Stream

Class upload_5xSoft

dim Form,File,Version

Private Sub Class_Initialize
dim iStart,iFileNameStart,iFileNameEnd,iEnd,vbEnter,iFormStart,iFormEnd,theFile
dim strDiv,mFormName,mFormValue,mFileName,mFileSize,mFilePath,iDivLen,mStr
Version=""
if Request.TotalBytes<1 then Exit Sub
set Form=CreateObject("Scripting.Dictionary")
set File=CreateObject("Scripting.Dictionary")
set upfile_5xSoft_Stream=CreateObject("Adodb.Stream")
upfile_5xSoft_Stream.mode=3
upfile_5xSoft_Stream.type=1
upfile_5xSoft_Stream.open
upfile_5xSoft_Stream.write Request.BinaryRead(Request.TotalBytes)

vbEnter=Chr(13)&Chr(10)
iDivLen=inString(1,vbEnter)+1
strDiv=subString(1,iDivLen)
iFormStart=iDivLen
iFormEnd=inString(iformStart,strDiv)-1
while iFormStart < iFormEnd
iStart=inString(iFormStart,"name=""")
iEnd=inString(iStart+6,"""")
mFormName=subString(iStart+6,iEnd-iStart-6)
iFileNameStart=inString(iEnd+1,"filename=""")
if iFileNameStart>0 and iFileNameStart<iFormEnd then
iFileNameEnd=inString(iFileNameStart+10,"""")
mFileName=subString(iFileNameStart+10,iFileNameEnd-iFileNameStart-10)
iStart=inString(iFileNameEnd+1,vbEnter&vbEnter)
iEnd=inString(iStart+4,vbEnter&strDiv)
if iEnd>iStart then
mFileSize=iEnd-iStart-4
else
mFileSize=0
end if
set theFile=new FileInfo
theFile.FileName=getFileName(mFileName)
theFile.FilePath=getFilePath(mFileName)
theFile.FileSize=mFileSize
theFile.FileStart=iStart+4
theFile.FormName=FormName
file.add mFormName,theFile
else
iStart=inString(iEnd+1,vbEnter&vbEnter)
iEnd=inString(iStart+4,vbEnter&strDiv)

if iEnd>iStart then
mFormValue=subString(iStart+4,iEnd-iStart-4)
else
mFormValue=""
end if
form.Add mFormName,mFormValue
end if

iFormStart=iformEnd+iDivLen
iFormEnd=inString(iformStart,strDiv)-1
wend
End Sub

Private Function subString(theStart,theLen)
dim i,c,stemp
upfile_5xSoft_Stream.Position=theStart-1
stemp=""
for i=1 to theLen
if upfile_5xSoft_Stream.EOS then Exit for
c=ascB(upfile_5xSoft_Stream.Read(1))
If c > 127 Then
if upfile_5xSoft_Stream.EOS then Exit for
stemp=stemp&Chr(AscW(ChrB(AscB(upfile_5xSoft_Stream.Read(1)))&ChrB(c)))
i=i+1
else
stemp=stemp&Chr(c)
End If
Next
subString=stemp
End function

Private Function inString(theStart,varStr)
dim i,j,bt,theLen,str
InString=0
Str=toByte(varStr)
theLen=LenB(Str)
for i=theStart to upfile_5xSoft_Stream.Size-theLen
if i>upfile_5xSoft_Stream.size then exit Function
upfile_5xSoft_Stream.Position=i-1
if AscB(upfile_5xSoft_Stream.Read(1))=AscB(midB(Str,1)) then
InString=i
for j=2 to theLen
if upfile_5xSoft_Stream.EOS then
inString=0
Exit for
end if
if AscB(upfile_5xSoft_Stream.Read(1))<>AscB(MidB(Str,j,1)) then
InString=0
Exit For
end if
next
if InString<>0 then Exit Function
end if
next
End Function

Private Sub Class_Terminate
form.RemoveAll
file.RemoveAll
set form=nothing
set file=nothing
upfile_5xSoft_Stream.close
set upfile_5xSoft_Stream=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 toByte(Str)
dim i,iCode,c,iLow,iHigh
toByte=""
For i=1 To Len(Str)
c=mid(Str,i,1)
iCode =Asc(c)
If iCode<0 Then iCode = iCode + 65535
If iCode>255 Then
iLow = Left(Hex(Asc(c)),2)
iHigh =Right(Hex(Asc(c)),2)
toByte = toByte & chrB("&H"&iLow) & chrB("&H"&iHigh)
Else
toByte = toByte & chrB(AscB(c))
End If
Next
End function
End Class


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

Public function SaveAs(FullPath)
dim dr,ErrorChar,i
SaveAs=1
if trim(fullpath)="" or FileSize=0 or FileStart=0 or FileName="" then exit function
if FileStart=0 or right(fullpath,1)="/" then exit function
set dr=CreateObject("Adodb.Stream")
dr.Mode=3
dr.Type=1
dr.Open
upfile_5xSoft_Stream.position=FileStart-1
upfile_5xSoft_Stream.copyto dr,FileSize
dr.SaveToFile FullPath,2
dr.Close
set dr=nothing
SaveAs=0
end function
End Class




response.write "<body bgcolor='#f3f3f3'>"

dim upload,file,formName,formPath,iCount,filename,fileExt,info
set upload=new upload_5xSoft ''建立上传对象
formPath="../loupan_imgs/" ''文件上传后存放的目录
if right(formPath,1)<>"/" then formPath=formPath&"/"
iCount=0
for each formName in upload.file ''列出所有上传了的文件
set file=upload.file(formName) ''生成一个文件对象

info = "无标题"
if upload.form("info")<>"" then
info = upload.form("info") '''''''''''''''''''''关键所在××××××××××××××××
response.write info
end if
if file.filesize<100 then
response.write "<font size=2>请先选择你要上传的图片 [ <a href=# onclick=history.go(-1)>重新上传</a> ]</font>"
response.end
end if

if file.filesize>100000 then
response.write "<font size=2>图片大小超过了限制 [ <a href=# onclick=history.go(-1)>重新上传</a> ]</font>"
response.end
end if

fileExt=lcase(right(file.filename,4))

if fileEXT<>".gif" and fileEXT<>".jpg" then
response.write "<font size=2>文件格式不对 [ <a href=# onclick=history.go(-1)>重新上传</a> ]</font>"
response.end
end if

filename=formPath&year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&file.FileName
filename1=year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&file.FileName
if file.FileSize>0 then ''如果 FileSize > 0 说明有文件数据
file.SaveAs Server.mappath(filename) ''保存文件
iCount=iCount+1
end if
set file=nothing
next
set upload=nothing ''删除此对象

session("upface")="done"

Htmend iCount&" 个文件上传结束!"

sub HtmEnd(Msg)
set upload=nothing

response.write "<script>"
response.write "document.write('<font size=2>图片上传成功,</font>');"
response.write "</script>"
end sub


%>
</body>
</html>
沙沙的吹 2003-08-24
  • 打赏
  • 举报
回复
我为什么不能结帖??
他说我帖子的回复次数大于给分次数,是什么意思/?
xuya 2003-08-24
  • 打赏
  • 举报
回复
实现图片上传并存入数据库中

用ASP编写网站应用程序时间长了,难免会遇到各式各样的问题,其中

关于如何上传文件到服务器恐怕是遇见最多的问题了,尤其是上传图片,比如

你想要在自己的社区里面实现类似网易虚拟社区提供的“每日一星”的功能,

就要提供给网友上传照片的功能。上传图片文件到服务器可以使用各种免费的

文件上传组件,使用起来功能虽然很强大,但是由于很多情况下,我们只能使

用免费的支持ASP的空间或者租用别人的虚拟空间,对于第一种情况,我们

根本就没有可能来使用文件上传组件;至于第二种情况,我们也要付出不少的

“银子”才可以。除非你拥有自己的虚拟主机,你就可以随便在服务器上面

安装自己所需要的组件,这种情况对于大多数人来说是可望而不可及的。那我

们就没有办法了吗?呵呵,答案是肯定的。下面就让我们一起来使用纯ASP
代码来实现图片的上传以及保存到数据库的功能(顺便也实现显示数据库中的
图片到网页上的功能)。

首先我们先来熟悉一下将要使用的对象方法。我们用来获取上一个页面传

递过来的数据一般是使用Request对象。同样的,我们也可以使用Request对象

来获取上传上来的文件数据,使用的方法是Request.BinaryRead()。而我们要从

数据库中读出来图片的数据显示到网页上面要用到的方法是:

Request.BinaryWrite()。在我们得到了图片的数据,要保存到数据库中的时候,

不可以直接使用Insert语句对数据库进行操作,而是要使用ADO的

AppendChunk方法,同样的,读出数据库中的图片数据,要使用GetChunk方

法。各个方法的具体语法如下:

* Request.BinaryRead语法:

variant = Request.BinaryRead(count)

参数

variant

返回值保存着从客户端读取到数据。

count

指明要从客户端读取的数据量大小,这个值小于或者等于使用方法

Request.TotalBytes得到的数据量。

* Request.BinaryWrite语法:

Request.BinaryWrite data

参数

data

要写入到客户端浏览器中的数据包。

* Request.TotalBytes语法:

variant = Request.TotalBytes

参数

variant

返回从客户端读取到数据量的字节数。

* AppendChunk语法

将数据追加到大型文本、二进制数据 Field 或 Parameter 对象。

object.AppendChunk Data

参数

object Field 或 Parameter 对象

Data 变体型,包含追加到对象中的数据。

说明

使用 Field 或 Parameter 对象的 AppendChunk 方法可将长二进制或字符数

据填写到对象中。在系统内存有限的情况下,可以使用 AppendChunk 方法对长

整型值进行部分而非全部的操作。

* GetChunk语法

返回大型文本或二进制数据 Field 对象的全部或部分内容 。

variable = field.GetChunk( Size )

返回值

返回变体型。

参数

Size 长整型表达式,等于所要检索的字节或字符数。

说明

使用 Field 对象的 GetChunk 方法检索其部分或全部长二进制或字符数据。

在系统内存有限的情况下,可使用 GetChunk 方法处理部分而非全部的长整型

值。

GetChunk 调用返回的数据将赋给“变量”。如果 Size 大于剩余的数据,则

GetChunk 仅返回剩余的数据而无需用空白填充“变量”。如果字段为空,则

GetChunk 方法返回 Null。

每个后续的 GetChunk 调用将检索从前一次 GetChunk 调用停止处开始的数

据。但是,如果从一个字段检索数据然后在当前记录中设置或读取另一个字段

的值,ADO 将认为已从第一个字段中检索出数据。如果在第一个字段上再次调

用 GetChunk 方法,ADO 将把调用解释为新的 GetChunk 操作并从记录的起始

处开始读取。如果其他 Recordset 对象不是首个 Recordset 对象的副本,则

访问其中的字段不会破坏 GetChunk 操作。

如果 Field 对象的 Attributes 属性中的 adFldLong 位设置为 True,则可

以对该字段使用 GetChunk 方法。

如果在 Field 对象上使用 Getchunk 方法时没有当前记录,将产生错误 3021

(无当前记录)。

接下来,我们就要来设计我们的数据库了,作为测试我们的数据库结构如

下(Access97):

字段名称    类型    描述

  id    自动编号   主键值

img OLE对象   用来保存图片数据 



对于在MS SQL Server7中,对应的结构如下:

字段名称    类型    描述

  id     int(Identity) 主键值

img   image     用来保存图片数据 



现在开始正式编写我们的纯ASP代码上传部分了,首先,我们有一个提

供给用户的上传界面,可以让用户选择要上传的图片。代码如下

(upload.htm):

<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>

注意代码中黑色斜体的部分,一定要在Form中有这个属性,否则,将无

法得到上传上来的数据。

接下来,我们要在process.asp中对从浏览器中获取的数据进行必要的处

理,因为我们在process.asp中获取到的数据不仅仅包含了我们想要的上传上来

的图片的数据,也包含了其他的无用的信息,我们需要剔除冗余数据,并将处

理过的图片数据保存到数据库中,这里我们以Access97为例。具体代

码如下(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={Microsoft Access Driver (*.mdb)};DBQ=" &

server.MapPath("images.mdb") & ";uid=;PWD=;"

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

%>

好了,这下我们就把上传来的图片保存到了名为images.mdb的数据库中

了,剩下的工作就是要将数据库中的图片数据显示到网页上面了。一般在HT

ML中,显示图片都是使用<IMG>标签,也就是<IMG SRC="图片路径">,但

是我们的图片是保存到了数据库中,“图片路径”是什么呢?呵呵,其实这个

SRC属性除了指定路径外,也可以这样使用哦:

<IMG SRC="showimg.asp?id=xxx">

所以,我们所要做的就是在showimg.asp中从数据库中读出来符合条件的

数据,并返回到SRC属性中就可以了,具体代码如下(showimg.asp):

<%

set connGraph=server.CreateObject("ADODB.connection")

connGraph.ConnectionString="driver={Microsoft Access Driver (*.mdb)};DBQ=" &

server.MapPath("images.mdb") & ";uid=;PWD=;"

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

%>

注意在输出到浏览器之前一定要指定Response.ContentType = "image/*",

以便正常显示图片。

最后要注意的地方是,我的process.asp中作的处理没有考虑到第一页

(upload.htm)中还有其他数据,比如<INPUT type=text name=userid>等等,如果

有这些项目,你的process.asp就要注意处理掉不必要的数据。

怎么样,其实上传图片并保存到数据库很简单吧,这样再也不用为自己的

空间无法使用各类的上传组件发愁了吧。
511214 2003-08-24
  • 打赏
  • 举报
回复
http://www.csdn.net/develop/Read_Article.asp?Id=19253
http://www.csdn.net/develop/Read_Article.asp?Id=19615
http://www.csdn.net/develop/Read_Article.asp?Id=19179
zhuyangfeng 2003-08-24
  • 打赏
  • 举报
回复
我要图片上传组件!
zyf911@tom.com
楚人无衣 2003-08-24
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/2159/2159717.xml?temp=5.466861E-02
沙沙的吹 2003-08-23
  • 打赏
  • 举报
回复
这个问题也不是很难嘛,为什么没有人回啊????
哎,小弟有时间就不到这里来问了!
沙沙的吹 2003-08-23
  • 打赏
  • 举报
回复
高手呢????
沙沙的吹 2003-08-23
  • 打赏
  • 举报
回复
qinyunchuan@163.com
zorou_fatal 2003-08-23
  • 打赏
  • 举报
回复
告诉邮箱,发个例子给你。

28,390

社区成员

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

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