当前位置:首页 » 文件管理 » asp图片上传

asp图片上传

发布时间: 2022-01-08 07:01:45

A. 急!!!asp如何上传图片

首先我们先来熟悉一下将要使用的对象方法。我们用来获取上一个页面传递过来的数据一般是使用Request对象。同样的,我们也可以使用Request对象来获取上传上来的文件数据,使用的方法是Request.BinaryRead()。而我们要从数据库中读出来图片的数据显示到网页上面要用到的方法是:
Request.BinaryWrite()。在我们得到了图片的数据,要保存到数据库中的时候,不可以直接使用Insert语句对数据库进行操作,而是要使用ADO的AppendChunk方法,同样的,读出数据库中的图片数据,要使用GetChunk方法。各个方法的具体语法如下:
*Request.BinaryRead语法:
variant=Request.BinaryRead(count)
参数
variant
返回值保存着从客户端读取到数据。
count
指明要从客户端读取的数据量大小,这个值小于或者等于使用方法Request.TotalBytes得到的数据量。
*Request.BinaryWrite语法:
Request.BinaryWritedata
参数
data
要写入到客户端浏览器中的数据包。
*Request.TotalBytes语法:
variant=Request.TotalBytes
参数
variant
返回从客户端读取到数据量的字节数。
*AppendChunk语法
将数据追加到大型文本、二进制数据Field或Parameter对象。
object.AppendChunkData
参数
objectField或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(无当前记录)。
接下来,我们就要来设计我们的数据库了,作为测试我们的数据库结构如下(access2000):

字段名称 类型 描述
id 自动编号 主键值
imgOLE对象 用来保存图片数据

对于在MSsqlServer7中,对应的结构如下:
字段名称 类型 描述
id int(Identity) 主键值
img image 用来保存图片数据

现在开始正式编写我们的纯ASP代码上传部分了,首先,我们有一个提供给用户的上传界面,可以让用户选择要上传的图片。代码如下
(upload.htm):
<html>
<body>
<center>
<form name="mainForm" enctype="multipart/form-data" action="process.asp" method=post>
<inputtype=filename=mefile><br>
<inputtype=submitname=okvalue="OK">
</form>
</center>
</body>
</html>
注意enctype="multipart/form-data",一定要在Form中有这个属性,否则,将无法得到上传上来的数据。接下来,我们要在process.asp中对从浏览器中获取的数据进行必要的处理,因为我们在process.asp中获取到的数据不仅仅包含了我们想要的上传上来的图片的数据,也包含了其他的无用的信息,我们需要剔除冗余数据,并将处理过的图片数据保存到数据库中,这里我们以access2000为例。具体代码如下(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)
setconnGraph=server.CreateObject("ADODB.connection")
connGraph.ConnectionString="driver={MicrosoftAccessDriver(*.mdb)};DBQ="&server.MapPath("images.mdb")&";uid=;PWD=;"
connGraph.Open
setrec=server.createobject("ADODB.recordset")
rec.Open"SELECT*FROM[images]whereidisnull",connGraph,1,3
rec.addnew
rec("img").appendchunkmydata
rec.update
rec.close
setrec=nothing
setconnGraph=nothing
%>
好了,这下我们就把上传来的图片保存到了名为images.mdb的数据库中了,剩下的工作就是要将数据库中的图片数据显示到网页上面了。一般在HTML中,显示图片都是使用<IMG>标签,也就是<IMGSRC="图片路径">,但是我们的图片是保存到了数据库中,“图片路径”是什么呢?呵呵,其实这个SRC属性除了指定路径外,也可以这样使用哦:
<IMGSRC="showimg.asp?id=xxx">
所以,我们所要做的就是在showimg.asp中从数据库中读出来符合条件的
数据,并返回到SRC属性中就可以了,具体代码如下(showimg.asp):
<%
setconnGraph=server.CreateObject("ADODB.connection")
connGraph.ConnectionString="driver={MicrosoftAccessDriver(*.mdb)};DBQ="&
server.MapPath("images.mdb")&";uid=;PWD=;"
connGraph.Open
setrec=server.createobject("ADODB.recordset")
strsql="selectimgfromimageswhereid="&trim(request("id"))
rec.openstrsql,connGraph,1,1
Response.ContentType="image/*"
Response.BinaryWriterec("img").getChunk(7500000)
rec.close
setrec=nothing
setconnGraph=nothing
%>
注意在输出到浏览器之前一定要指定Response.ContentType="image/*",
以便正常显示图片。

B. asp如何实现文件上传功能

基本原理是:采用ADO Stream对象的BinaryRead方法将FORM中的所有数据读出,从中截取出所需的文件数据,以二进制文件方式存盘。

下面是上传文件页面的一个例子:

<html>

<body>

<form name="Upload" Method="Post" Enctype="multipart/form-data" Action="Upload.asp">

<input type="file" name="FileName">

<INPUT TYPE="Submit" VALUE="Upload"></TD>

</form>

</body>

</html>

(2)asp图片上传扩展阅读

几种文件上传技术的比较

1、基于HTTP协议

该方法需要编程者利用第三方软件,如DELPHI、VB等,在应用程序中先进行HTTP协议编程,然后将待上传文件内容按HTTP协议的格式打包,最后向WEB服务器发送上传的请求报文,从而实现文件的上传。

因为DELPHI和VB不能编写完整的WEB网络程序,只能编写WEB小应用程序,因此,该方法只用于功能受限的网络应用。

2、基于VB(或DELPHI等)开发的文件上传组件

该方法利用VB(或DELPHI等编程语言)开发ASP服务器组件,实现特定的文件上传服务。它首先利用ASP表单功能将文件(二进制格式)从用户端上传到服务器端,然后使用VB开发的组件,对二进制文件进行处理,成为可以正常读写的文件。

该方法要求编程者不仅掌握ASP语言,而且还能利用VB等第三方语言进行组件编程,增加了开发的难度。

3、基于数据库技术

该方法和上个方法有类似之处。不同的地方在于对上传的二进制文件的处理上。它使用数据库来保存二进制文件。无论是小型数据库还是大型数据库都提供了存储二进制数据的数据类型,只要以Append Chunk方式将数据存入相应的字段就可以了。

该方法虽然简单可行,但是因为每次上传的文件大小都是不一样的,因此,会对数据库的空间造成很大的浪费,降低了数据的访问速度;并且使得文件只能在数据库环境下进行访问,造成了很大的不便。

C. [急!]怎样用asp实现图片上传功能呢

无惧无组件上传,好用,你可以试试,自已修改也行

D. ASP图片上传

建四个文件:
1: index.asp

<link href="../css/bodycss.css" rel="stylesheet" type="text/css"><table width="600" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><table width="363" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="363" class="left"><form name="cn_bruce" method="POST" action="">
<textarea cols="50" name="cn_content" rows="2" width="100%"></textarea>
</form></td>
</tr>
</table>
<p class="left"></p></td>
<td><iframe border="0" frameBorder="0" noResize scrolling="no" width="100%" src="upload.asp"></iframe></td>
</tr>
</table>

2: upfile.asp

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#include file="upload.inc"-->
<html>
<head>
<title>文件上传</title>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
</head>
<body>
<table width="600" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><%
dim upload,f_folder,file,formPath,iCount,filename,fileExt,filesizemin,filesizemax

filesizemin=100
filesizemax=100000*10000
set upload=new upload_5xSoft '建立上传对象
f_folder=upload.form("upfilefolder")

'********************************列出所有上传文件***************************************************
For each formName in upload.objFile
set file=upload.file(formName)
If file.filesize>0 then

'********************************检测文件大小***************************************************
If file.filesize<filesizemin Then
response.write "你上传的文件太小了 [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
ElseIf file.filesize>filesizemax then
response.write "文件大小超过了 "&filesizemax&"字节 限制 [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
End If

'********************************检测文件类型****************************************************
fileExt=ucase(right(file.filename,4))
uploadsuc=false
Forum_upload="RAR|JPG|GIF|"
Forumupload=split(Forum_upload,"|")
for i=0 to ubound(Forumupload)
if fileEXT="."&trim(Forumupload(i)) then
uploadsuc=true
exit for
else
uploadsuc=false
end if
next
if uploadsuc=false then
response.write "文件格式不正确 [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
response.end
end if

'********************************建立文件上传的目录文件夹****************************************
Set upf=Server.CreateObject("Scripting.FileSystemObject")
If Err<>0 Then
Err.Clear
response.write("您的服务器不支持FSO")
response.end
End If
f_type= replace(fileExt,".","")
f_name= year(now)&"-"&month(now)
If upf.FolderExists(Server.MapPath(f_folder&"/"&f_type&"/"&f_name))=False Then
If upf.FolderExists(Server.MapPath(f_folder&"/"&f_type))=False Then
If upf.FolderExists(Server.MapPath(f_folder))=False Then
upf.CreateFolder Server.MapPath(f_folder)
upf.CreateFolder Server.MapPath(f_folder&"/"&f_type)
upf.CreateFolder Server.MapPath(f_folder&"/"&f_type&"/"&f_name)
Else
upf.CreateFolder Server.MapPath(f_folder&"/"&f_type)
upf.CreateFolder Server.MapPath(f_folder&"/"&f_type&"/"&f_name)
End If
Else
upf.CreateFolder Server.MapPath(f_folder&"/"&f_type&"/"&f_name)
End If
End If
f_ftn=f_folder&"/"&f_type&"/"&f_name
Set upf=Nothing

'********************************保存上传文件至文件夹*****************************************
randomize
ranNum=int(90000*rnd)+10000
filename=f_ftn&"/"&day(now)&"-"&ranNum&"-"&file.filename
if file.filesize>filesizemin and file.filesize<filesizemax then
file.SaveAs Server.mappath(filename) '保存文件
if f_type="JPG" or f_type="GIF" or f_type="PNG" then
response.write "<script>parent.cn_bruce.cn_content.value+='[img]"&filename&"[/img]'</script>"
ElseIf f_type="ZIP" or f_type="RAR" or f_type="DOC" or f_type="TXT" then
response.write "<script>parent.cn_bruce.cn_content.value+='[url]"&filename&"[/url]'</script>"
'ElseIf
else
response.write "<script>parent.cn_bruce.cn_content.value+=' "&filename&" '</script>"
end if
iCount=iCount+1
end if
set file=nothing
end if
next
set upload=nothing '删除此对象

response.write (iCount&" 个文件上传成功! <a href=# onclick=history.go(-1)>继续上传</a>")
%></td>
</tr>
</table>
</body>
</html>

3: upload.asp

<meta http-equiv="content-type" content="text/html;charset=gb2312">
<link href="../css/bodycss.css" rel="stylesheet" type="text/css">
<table width="245" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><form name="form" method="post" action="upfile.asp" enctype="multipart/form-data">
<input type="hidden" name="upfilefolder" value="uploadfile">
<input type="hidden" name="act" value="upload">
<input type="file" name="file11" size=10>
<br>
<input type="file" name="file12" size=10>
<br>
<input type="file" name="file13" size=10>
<input type="submit" name="Submit" value="上传">
</form></td>
</tr>
</table>

4: upload.inc
<link href="../css/bodycss.css" rel="stylesheet" type="text/css">
<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.to dr,FileSize
dr.SaveToFile FullPath,2
dr.Close
set dr=nothing
SaveAs=false
end function
End Class
</SCRIPT>

E. ASP 如何上传图片

利用这个组件就可以实现上传了,里面已经写好了上传代码,你直接使用即可。

F. asp上传图片

-----conn.asp----
<%
db_path="db.mdb"
set conn=server.CreateObject("ADODB.connection")
connstr="driver={Microsoft Access Driver (*.mdb)};dbq="&server.MapPath(db_path)
conn.open connstr
%>
--------index.asp-----
<!--#include file="conn.asp"-->
【<a href="upload.asp">上传图片</a>】<br><br>
<%
strsql="select * from imgurl"
set rs=server.createobject("ADODB.recordset")
rs.open strsql,conn,1,1
do until rs.eof
%>
<img src="showimg.asp?id=<%=rs("id")%>"><br>
<%
rs.movenext
loop
rs.close
conn.close
%>
<br><br>【<a href="upload.asp">上传图片</a>】
-----save.asp--------
<!--#include file="conn.asp"-->
<%
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 rs=server.createobject("ADODB.recordset")
rs.open "SELECT * FROM imgurl",conn,3,3
rs.addnew
rs("img").AppendChunk myData
rs.update
rs.close
set rs=nothing
set conn=nothing
response.redirect "index.asp"
%>
-----showimg.asp--------
<!--#include file="conn.asp"-->
<%
set rs=server.createobject("ADODB.recordset")
sql="select * from imgurl where id="&trim(request("id"))
rs.open sql,conn,1,1
Response.ContentType="image/*"
Response.BinaryWrite rs("img").getChunk(8000000)
rs.close
set rs=nothing
set conn=nothing
%>
-----upload.asp--------
<form action="save.asp" method="post" enctype="multipart/form-data">
<input type="file" name="imgurl">
<input type="submit" name=ok value="提交">
</form>
数据库:db.mdb
表名:imgurl
id img
自动编号 长二进制数据

G. ASP上传图片功能

<!--#includefile="conn.asp"-->
<%
setrs=server.createobject("ADODB.recordset")
sql="select*fromimgurl"
rs.opensql,conn,1,1
whatid=rs("id")
dountilrs.eof
%>
<imgsrc="showimg.asp?id=<%=whatid%>">
<%
rs.movenext
loop%>

<img src="showimg.asp?id=<%=whatid%>">你这个是一个图片地址么?还是在showimg.asp里又加了调用

H. ASP如何上传图片,并且可以添加图片说明

用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.BinaryWritedata
参数
data
要写入到客户端浏览器中的数据包。
*Request.TotalBytes语法:
variant=Request.TotalBytes
参数
variant
返回从客户端读取到数据量的字节数。
*AppendChunk语法
将数据追加到大型文本、二进制数据Field或Parameter对象。
object.AppendChunkData
参数
objectField或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将把调用解释为新的GetChu
nk操作并从记录的起始处开始读取。如果其他Recordset对象不是首个Recordset对象的副本,
则访问其中的字段不会破坏GetChunk操作。
如果Field对象的Attributes属性中的adFldLong位设置为True,则可以对该字段使用GetChun
k方法。
如果在Field对象上使用Getchunk方法时没有当前记录,将产生错误3021(无当前记录)。
接下来,我们就要来设计我们的数据库了,作为测试我们的数据库结构如下(Access200
0):

字段名称 类型 描述
id 自动编号 主键值
img OLE对象 用来保存图片数据

对于在MSSQLServer7中,对应的结构如下:
字段名称 类型 描述
id int(Identity) 主键值
img image 用来保存图片数据

现在开始正式编写我们的纯ASP代码上传部分了,首先,我们有一个提供给用户的上传界面
,可以让用户选择要上传的图片。代码如下
(upload.htm):
<html>
<body>
<center>
<form name="mainForm" enctype="multipart/form-data" action="process.asp" method=p
ost>
<inputtype=filename=mefile><br>
<inputtype=submitname=okvalue="OK">
</form>
</center>
</body>
</html>
注意enctype="multipart/form-data",一定要在Form中有这个属性,否则,将无法得到上传
上来的数据。接下来,我们要在process.asp中对从浏览器中获取的数据进行必要的处理,因
为我们在process.asp中获取到的数据不仅仅包含了我们想要的上传上来的图片的数据,也包
含了其他的无用的信息,我们需要剔除冗余数据,并将处理过的图片数据保存到数据库中,这
里我们以access2000为例。具体代码如下(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)
setconnGraph=server.CreateObject("ADODB.connection")
connGraph.ConnectionString="driver={MicrosoftAccessDriver(*.mdb)};DBQ="&server.Ma
pPath("images.mdb")&";uid=;PWD=;"
connGraph.Open
setrec=server.createobject("ADODB.recordset")
rec.Open"SELECT*FROM[images]whereidisnull",connGraph,1,3
rec.addnew
rec("img").appendchunkmydata
rec.update
rec.close
setrec=nothing
setconnGraph=nothing
%>
好了,这下我们就把上传来的图片保存到了名为images.mdb的数据库中了,剩下的工作就是要
将数据库中的图片数据显示到网页上面了。一般在HTML中,显示图片都是使用<IMG>标签
,也就是<IMGSRC="图片路径">,但是我们的图片是保存到了数据库中,“图片路径”是什么
呢?呵呵,其实这个SRC属性除了指定路径外,也可以这样使用哦:
<IMGSRC="showimg.asp?id=xxx">
所以,我们所要做的就是在showimg.asp中从数据库中读出来符合条件的
数据,并返回到SRC属性中就可以了,具体代码如下(showimg.asp):
<%
setconnGraph=server.CreateObject("ADODB.connection")
connGraph.ConnectionString="driver={MicrosoftAccessDriver(*.mdb)};DBQ="&
server.MapPath("images.mdb")&";uid=;PWD=;"
connGraph.Open
setrec=server.createobject("ADODB.recordset")
strsql="selectimgfromimageswhereid="&trim(request("id"))
rec.openstrsql,connGraph,1,1
Response.ContentType="image/*"
Response.BinaryWriterec("img").getChunk(7500000)
rec.close
setrec=nothing
setconnGraph=nothing
%>
注意在输出到浏览器之前一定要指定Response.ContentType="image/*",
以便正常显示图片。
最后要注意的地方是,我的process.asp中作的处理没有考虑到第一页(upload.htm)中还有其
他数据,比如<INPUT type=tesxt name=userid>等等,如果有这些项目,你的process.asp就
要注意处理掉不必要的数据。

I. asp 上传图片 代码

我嘴笨 代码上!
protected void UpLoad(object sender, EventArgs e)
{
foreach (UploadedFile file in RadUploadContext.Current.UploadedFiles)
{
string Path = Server.MapPath(@"../../Uploads");

//如果路径不存在,则创建
if (System.IO.Directory.Exists(Path) == false)
{
System.IO.Directory.CreateDirectory(Path);
}

//file.GetName()取得文件名
string filename = file.GetName().ToString();

//取得文件名(包括路径)里最后一个"."的索引
int index = filename.LastIndexOf(".");
//取得文件扩展名
string extendName = filename.Substring(index);

//取得原文件名不包含后缀名
string fileNameFirst = filename.Substring(0, index);

//用当前时间为文件重名名,确保文件名不重复
string datename = DateTime.Now.ToString("yyyyMMddHHmmss");

string newFileName = fileNameFirst + datename + extendName;

//组合路径
Path = Path + "/" + newFileName;

//保存
file.SaveAs(Path, true);

this.dataurl.Visible = true;
this.lab2.Visible = true;
this.FileUpload1.Visible = false;
this.Button3.Visible = false;

this.dataurl.ReadOnly=true;
this.dataurl.Text = newFileName;

//Response.Write("f1:" + fileNameFirst);
//Response.Write("f2:" + Path);
}
}
前台:
<th>上传文件:</th>
<td class="style1">
<asp:TextBox runat="server" ID ="dataurl" Width="50%" Visible="false"></asp:TextBox>
<asp:FileUpload ID="FileUpload1" runat="server" Width="65%"/>
<asp:Button ID="Button3" runat="server" Text="上传" OnClick="UpLoad" Height="25" Width="25%"/>
<asp:Label ID ="lab2" runat="server" Text="上传成功!" ForeColor="Red" Visible="false"></asp:Label>
</td>

J. 如何asp图片上传

首先了解一下在程序中用到的各种对象及其语法:

1) Request.BinaryRead()方法

● 使用Request.BinaryRead()方法可以获取提交的文件数据

● 语法

VarReValue= Request.BinaryRead(number)

变量VarReValue返回值保存从客户端读取到的二进制数据;

参数number指明要从客户端读取的二进制数据量的大小。

2) Response.BinaryWrite()方法

● 使用Response.BinaryWrite()方法可以从数据库中获取图片数据并显示到客户端的浏览器中。

● 语法

Response.BinaryWrite data

参数data是要写进客户端浏览器中的二进制数据包。

3) AppendChunk方法

● AppendChunk方法的作用是将二进制数据追加到Field或Parameter对象。

● 语法

object.AppendChunk data

参数data为要追加到Field或Parameter对象中的数据包。

4) GetChunk方法

● GetChunk方法返回二进制数据的内容。

● 语法

object. GetChunk(size)

参数size指明要返回二进制数据的长度,可以是长整型表达式。

5) Request.TotalBytes方法

● Request.TotalBytes方法返回从客户端读取到的数据的字节数,这个值跟上面所提到的number相对应,可以大于或等于number值。

● 语法

number= Request.TotalBytes

大体了解了一些方法及其使用方法后,接下来我们就开始设计数据库和相关编写代码了。

第一步:数据库的设计(以Ms SQL Server7为例):

Create table img --创建用来存储图片的表,命名为img

(

id int identity(1,1) not null,

img image

)
第二步:程序编写,其中省略了用户输入界面,这里只给出很重要的两个文件即图片上传处理(processimg.asp)和显示图片(ShowImg.asp)文件。

1) processimg.asp文件代码:

〈%

Response.Buffer=True

ImageSize=Request.TotalBytes ‘获取提交数据量的总字节数

ImageData=Request.BinaryRead(ImageSize) ‘保存从客户端读取到的数据

‘优化读取到的二进制数据

BnCrLf=chrB(13)&chr(10)

Divider=LeftB(ImageData,Clng(InstrB(ImageData, BnCrLf))-1)

Dstart=InstrB(ImageData, BnCrLf& BnCrLf)+4

Dend=InstrB(Dstart+1, ImageData, Divider)- Dstart

MyData=MidB(ImageData, Dstart, Dend)

‘创建对象实例

Set imgConn=Server.CreateObject(“ADODB.Connection”)

StrConn=”Driver={SQL Server};Server=ServerName;”& _

“Uid=xxxx;Pwd=xxxx;DataBase=DataBaseName”

imgConn.open strConn

Set Rs= Server.CreateObject(“ADODB.RecordSet”)

Sql=”Select * From img Where id is null”

Rs.open sql,imgConn,1,3

‘追加数据到数据库

Rs.AddNew

Rs(“img”).AppendChunk myData

Rs.Update

‘关闭和释放对象

Rs.close

ImgConn.close

Set Rs=Nothing

Set ImgConn=Nothing

%〉

2) ShowImg.asp文件代码:

〈%

Response.Expires = 0

Response.buffer=True

Response.clear

‘创建对象实例

Set imgConn=Server.CreateObject(“ADODB.Connection”)

StrConn=”Driver={SQL Server};Server=ServerName;”& _

“Uid=xxxx;Pwd=xxxx;DataBase=DataBaseName”

imgConn.open strConn

Set Rs= Server.CreateObject(“ADODB.RecordSet”)

Sql=”Select img From img Where id=1” 这里的id可以使用Request(“id”)获得

Rs.open sql,imgConn,1,1

Response.ContentType=”image/*”

Response.BinaryWrite Rs.(“img”).GetChunk(7500000)

‘关闭和释放对象

Rs.close

ImgConn.close

Set Rs=Nothing

Set ImgConn=Nothing

%〉

至此,本文对如何使用ASP上传图片的原理和示例都讲完了,有什么不妥的地方请各位指正,谢谢!

热点内容
数字支付密码哪里找 发布:2024-05-04 14:26:50 浏览:84
天翼云免费存储 发布:2024-05-04 14:22:55 浏览:780
微信56g缓存怎么解决 发布:2024-05-04 14:09:41 浏览:705
sqlupdatewhereand 发布:2024-05-04 13:55:47 浏览:585
java视频教程推荐 发布:2024-05-04 13:55:08 浏览:85
安卓官服闪耀暖暖怎么换 发布:2024-05-04 13:46:37 浏览:170
我的世界精灵服务器怎么抓宠物 发布:2024-05-04 13:28:54 浏览:959
编译androidwebkit 发布:2024-05-04 13:11:37 浏览:761
安卓微信流量怎么控制 发布:2024-05-04 12:47:19 浏览:799
mysql主从复制数据库 发布:2024-05-04 12:37:55 浏览:512