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

asp上传图片

发布时间: 2022-01-11 00:00:07

1. ASP 如何上传图片

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

2. asp上传单个图片的最简单代码是什么

需要三个文件:
第一个(upload.asp)
<% Response.Buffer=True %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<title>图片上传</title>
</head>
<body background="images/bg.gif" topmargin="30" style=""><div align="center">
<center>
<table border="1" cellspacing="1" style="border-collapse: collapse" bordercolor="#C0C0C0" width="420" id="AutoNumber69" bgcolor="#F5F5F5">
<tr>
<td width="100%" bgcolor="#F5F5F5" bordercolor="#F5F5F5"><form name="form1" method="post" action="uploadsave.asp" enctype="multipart/form-data">
<p align="center"><span style="font-size: 12px">图片上传:</span><br>
<br>

<input type="file" class="f11" name="file1" size="23">
<br>
<br>
<input type=submit name="submit" value="上 传" class=anniu >
<br>
<br>
<span style="font-size: 12px">文件格式为<font color="#EC5106">*.gif,*.jpg,*.jpeg
</font></span></p>
</form></td>
</tr>
</table>
</center>
</body>
</html>
第二个文件(uploadsave.asp)
<% Response.Buffer=True %>

<html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>图片上传</title>

</head>

<body topmargin="20">
<!--#include FILE="upload_asjob.asp"-->
<%
set upload=new upload_5xsoft
set file=upload.file("file1")
if file.fileSize>0 then
'自动生成文件名
filename=date()
filename=filename&time()
filename=replace(filename,"-","")
filename=replace(filename,":","")
filename=replace(filename," ","")
filename=filename+"."
filenameend=file.filename
filenameend=split(filenameend,".")
if filenameend(1)="gif" or filenameend(1)="jpg" or filenameend(1)="jpeg" then
filename=filename&filenameend(1)
file.saveAs Server.mappath("uppic/"&filename)
response.write "<p align=center>"
response.write "图片已成功上传至服务器中,请使用下面红色地址添加图片,<br><p align=center><a href=upload.asp>点击这里重新上传图片!</a>"
response.write "</p>"
response.write "<p align=center>"
response.write "<font color=red>"
response.write "asjobcom/uppic/"&filename
response.write "</font></p>"
response.write "<p align=center>"
response.write "<a href='uppic/"&filename&"' target=_blank>点击这里查看上传的图片</a>"
response.write "</p>"
response.write "<p align=center><a href=javascript:window.close()>【关闭窗口】</a>"
else
response.write "文件格式不对"
response.write "<a href=upload.asp>返回"
end if
set file=nothing
else
response.write "文件内容不能为空"
response.write "<a href=upload.asp>返回"
end if
set upload=nothing
response.write "<br>"
%>
最后一个(upload_asjob.asp)
<SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT>

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="化境编程界HTTP上传程序 Version 1.0"
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.to dr,FileSize
dr.SaveToFile FullPath,2
dr.Close
set dr=nothing
SaveAs=0
end function
End Class
</SCRIPT>

3. asp做上传图片

需要三个文件:
第一个(upload.asp)
<% Response.Buffer=True %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<title>图片上传</title>
</head>
<body background="images/bg.gif" topmargin="30" style=""><div align="center">
<center>
<table border="1" cellspacing="1" style="border-collapse: collapse" bordercolor="#C0C0C0" width="420" id="AutoNumber69" bgcolor="#F5F5F5">
<tr>
<td width="100%" bgcolor="#F5F5F5" bordercolor="#F5F5F5"><form name="form1" method="post" action="uploadsave.asp" enctype="multipart/form-data">
<p align="center"><span style="font-size: 12px">图片上传:</span><br>
<br>

<input type="file" class="f11" name="file1" size="23">
<br>
<br>
<input type=submit name="submit" value="上 传" class=anniu >
<br>
<br>
<span style="font-size: 12px">文件格式为<font color="#EC5106">*.gif,*.jpg,*.jpeg
</font></span></p>
</form></td>
</tr>
</table>
</center>
</body>
</html>
第二个文件(uploadsave.asp)
<% Response.Buffer=True %>

<html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>图片上传</title>

</head>

<body topmargin="20">
<!--#include FILE="upload_asjob.asp"-->
<%
set upload=new upload_5xsoft
set file=upload.file("file1")
if file.fileSize>0 then
'自动生成文件名
filename=date()
filename=filename&time()
filename=replace(filename,"-","")
filename=replace(filename,":","")
filename=replace(filename," ","")
filename=filename+"."
filenameend=file.filename
filenameend=split(filenameend,".")
if filenameend(1)="gif" or filenameend(1)="jpg" or filenameend(1)="jpeg" then
filename=filename&filenameend(1)
file.saveAs Server.mappath("uppic/"&filename)
response.write "<p align=center>"
response.write "图片已成功上传至服务器中,请使用下面红色地址添加图片,<br><p align=center><a href=upload.asp>点击这里重新上传图片!</a>"
response.write "</p>"
response.write "<p align=center>"
response.write "<font color=red>"
response.write "asjobcom/uppic/"&filename
response.write "</font></p>"
response.write "<p align=center>"
response.write "<a href='uppic/"&filename&"' target=_blank>点击这里查看上传的图片</a>"
response.write "</p>"
response.write "<p align=center><a href=javascript:window.close()>【关闭窗口】</a>"
else
response.write "文件格式不对"
response.write "<a href=upload.asp>返回"
end if
set file=nothing
else
response.write "文件内容不能为空"
response.write "<a href=upload.asp>返回"
end if
set upload=nothing
response.write "<br>"
%>
最后一个(upload_asjob.asp)
<SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT>

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="化境编程界HTTP上传程序 Version 1.0"
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.to dr,FileSize
dr.SaveToFile FullPath,2
dr.Close
set dr=nothing
SaveAs=0
end function
End Class
</SCRIPT>

4. 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里又加了调用

5. 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就
要注意处理掉不必要的数据。

6. ASP里面如何上传图片

1.如果服务器装了文件上传组件,可查阅一下该组件的相关属性和方法,不过现在很多服务器空间都不支持这类组件.暂不在这里讨论.

2.若服务器不支持文件上传组件,可以考虑自己写个无组件上传程序,网上有一个"化境无组件上传"比较经典,也比较好用.地址:http://www.5xsoft.com/Down.aspx?id=2.
你可以下载一个参考,里面的说明很详细.
不过可能有的服务器也不会支持,因为该组件可能会被视为木马之类而被清除.

3.利用服务器的FSO组件可实现文本文件之类的在线读写,但要实现上传图片估计不能,我还没见过.

4.以上办法都不行时,我们可以考虑将图片上传到数据库.下面以Access为例,看一个简单的代码:

首先建一个数据库,假设为(img.mdb).在其中建一表(pic),表中有两个字段(id,img),id数据类型为"自动编号",img的类型为"OLE对象".
下面用三个文件用来实现图片功能,conn.asp是定义数据库连接函数的,up.asp用来上传图片,show.asp用来从数据库读取图片.

-------------conn.asp--------------
<%
dim conn,rs

function getdata(sql)
dbpath="img.mdb"
set conn=server.CreateObject("ADODB.connection")
connstr="provider=Microsoft.jet.oledb.4.0;data source="&server.MapPath(dbpath)
conn.open connstr
set rs=server.CreateObject("ADODB.recordset")
rs.open sql,conn,3,2
end function

sub rsclose()
rs.close()
set rs=nothing
conn.close()
set conn=nothing
end sub
%>

---------------up.asp---------------
<html>
<body>
<form action="up.asp" method="post" enctype="multipart/form-data" >
<input type="file" size="12" name="imgurl" id="imgurl">
<input type="submit" value="upload">
</form>
</body>
</html>
<%
if (request.totalbytes)>0 then '如果有数据提交,则进行下面的处理
%>
<!--#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)
sql="select * from pic"
getdata(sql)
rs.addnew
rs("img").AppendChunk myData
rs.update
rsclose()
response.clear
response.write "success!"
end if
%>

-------------show.asp--------------
<!--#include file="conn.asp"-->
<%
id=trim(request("id"))
sql="select * from pic where id="&id
getdata(sql)
Response.ContentType="image/*"
Response.BinaryWrite rs("img").getChunk(8000000)
rsclose()
%>

图片上传后就可以通过show.asp?id=*来读取了,你也可以直接将图片用<img>标签插入其他页面中,如<img src="show.asp?id=1" />

不好意思,未做注释,有不懂的先到网上搜一下,不行再联系我吧

7. 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
自动编号 长二进制数据

8. 如何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上传图片的原理和示例都讲完了,有什么不妥的地方请各位指正,谢谢!

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

(9)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方式将数据存入相应的字段就可以了。

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

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

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

热点内容
ftp服务器并发数量 发布:2024-09-28 10:19:02 浏览:542
只编译一个c文件 发布:2024-09-28 09:54:39 浏览:237
指纹密码怎么破 发布:2024-09-28 09:45:11 浏览:661
自编自选脚本 发布:2024-09-28 09:45:10 浏览:932
androidui教程pdf 发布:2024-09-28 09:44:13 浏览:899
iphone排列文件夹 发布:2024-09-28 09:30:46 浏览:355
安卓用什么自拍 发布:2024-09-28 09:20:14 浏览:384
python声明列表 发布:2024-09-28 08:54:52 浏览:15
全自动密码指纹锁哪里批发 发布:2024-09-28 08:54:51 浏览:608
移动服务密码怎么查询 发布:2024-09-28 08:54:47 浏览:479