當前位置:首頁 » 文件管理 » 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實現圖片上傳功能呢

無懼無組件上傳,好用,你可以試試,自已修改也行

熱點內容
java做資料庫 發布:2024-09-28 12:02:37 瀏覽:872
ssid信息如何配置 發布:2024-09-28 11:15:10 瀏覽:814
下載為什麼要鎖屏密碼 發布:2024-09-28 11:10:59 瀏覽:694
圖像雙線性插值演算法 發布:2024-09-28 11:06:31 瀏覽:872
sql怎麼執行存儲過程 發布:2024-09-28 10:44:32 瀏覽:48
ftp伺服器並發數量 發布:2024-09-28 10:19:02 瀏覽:545
只編譯一個c文件 發布:2024-09-28 09:54:39 瀏覽:240
指紋密碼怎麼破 發布:2024-09-28 09:45:11 瀏覽:663
自編自選腳本 發布:2024-09-28 09:45:10 瀏覽:933
androidui教程pdf 發布:2024-09-28 09:44:13 瀏覽:901