當前位置:首頁 » 文件管理 » 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-04-25 12:35:04 瀏覽:558
glinux下載 發布:2024-04-25 12:30:09 瀏覽:83
安卓手機可以用的谷歌叫什麼 發布:2024-04-25 12:05:57 瀏覽:942
linux改變用戶所屬組 發布:2024-04-25 11:50:33 瀏覽:469
rsa加密演算法java代碼 發布:2024-04-25 11:40:07 瀏覽:883
如何改變拉桿箱上的初始密碼 發布:2024-04-25 11:17:23 瀏覽:799
內網掛代理虛擬機如何配置網卡 發布:2024-04-25 11:15:06 瀏覽:687
明日之後緩存怎麼清理 發布:2024-04-25 11:14:56 瀏覽:205
華為mate30怎麼退回安卓版 發布:2024-04-25 11:08:49 瀏覽:898
安卓新機使用前要注意什麼 發布:2024-04-25 11:03:46 瀏覽:811