當前位置:首頁 » 文件管理 » asp實現圖片上傳

asp實現圖片上傳

發布時間: 2023-06-06 02:37:04

『壹』 ASP中上傳圖片怎麼做啊

採用無組件下載代碼。
upfile.asp

<% 'OPTION EXPLICIT%>
<!--#include FILE="upload_5xsoft.inc"-->

<%
dim upload,file,formName,formPath,iCount,MyFile
set upload=new upload_5xSoft ''建立上傳對象

formpath="userdata/" ''得到上傳目錄

nian=Year(Date)
yue=Month(Date)
ri=Day(Date)
if len(nian)<4 then nian="00"&nian
if len(yue)<2 then yue="0"&yue
if len(ri)<2 then ri="0"&ri

ipinit=""
ip=Request.ServerVariables("REMOTE_ADDR")
tmpip=Split(ip,".")
tmpipb=UBound(tmpip)
' ReDim ThisIP(tmpipb)
for i=0 to tmpipb
ThisIp=tmpip(i)

Do WHile len(ThisIP)<3
ThisIP="0"+ThisIP
loop
ipinit=ipinit+ThisIP
next

'Response.write nian&yue&ri&ipinit
'Response.end

for each formName in upload.file ''列出所有上傳了的文件
set file=upload.file(formName) ''生成一個文件對象
if file.FileSize>0 then ''如果 FileSize > 0 說明有文件數據
'MyFile=formPath&File.FileName
'MyFile=File.FileName

shi=Hour(time)
fen=Minute(Time)
Miao=Second(Time)
if len(shi)<2 then shi="0"&shi
if len(fen)<2 then fen="0"&fen
if len(miao)<2 then miao="0"&miao

MyFile=nian&yue&ri&shi&fen&miao&ipinit&".txt"

file.SaveAs Server.mappath(formPath&MyFile) ''保存文件
response.write file.FilePath&"#"&file.FileName&"#"&" ("&file.FileSize&") => "&MyFile&" 成功!<br>"
end if
set file=nothing
next
set upload=nothing ''刪除此對象

%>

upload_5xsoft.inc

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

『貳』 asp如何實現圖片和視頻批量上傳

ASP批量上傳圖片代碼風聲無組件上傳類'上傳界面 up.asp<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%><!--#include file="../Connections/conn.asp" --><%
'主菜單
Dim zpmenurs
Dim zpmenurs_numRowsSet zpmenurs = Server.CreateObject("ADODB.Recordset")
zpmenurs.ActiveConnection = MM_conn_STRING
zpmenurs.Source = "SELECT * FROM menu_2 WHERE menu=3"
zpmenurs.CursorType = 0
zpmenurs.CursorLocation = 2
zpmenurs.LockType = 1
zpmenurs.Open()zpmenurs_numRows = 0
%><%'次級菜單
Dim zpchildrs
Dim zpchildrs_numRowsSet zpchildrs = Server.CreateObject("ADODB.Recordset")
zpchildrs.ActiveConnection = MM_conn_STRING
zpchildrs.Source = "SELECT * FROM menu_2child"
zpchildrs.CursorType = 0
zpchildrs.CursorLocation = 2
zpchildrs.LockType = 1
zpchildrs.Open()zpchildrs_numRows = 0
%>//連動菜單腳本 如果不做連動菜單可以刪除//////////////////////////////////////////-------------開始----------------------------------------------------------------------
<SCRIPT language=javascript1.2>
function showsubmenu(sid)
{
whichel = eval("submenu" + sid);
if (whichel.style.display == "none")
{
eval("submenu" + sid + ".style.display='';");
}
else
{
eval("submenu" + sid + ".style.display='none';");
}
}
</SCRIPT><SCRIPT language=JavaScript type=text/javascript>
<!--var arrDynaList = new Array();
var arrDL1 = new Array();arrDL1[1] = "mainmenu"; // Name of parent list box
arrDL1[2] = "picform1"; // Name of form containing parent list box
arrDL1[3] = "submenu"; // Name of child list box
arrDL1[4] = "picform1"; // Name of form containing child list box
arrDL1[5] = arrDynaList;

<%
Dim txtDynaListRelation, txtDynaListLabel, txtDynaListValue, = "menu_father" // Name of recordset field relating to parent
txtDynaListLabel = "menu_child" // Name of recordset field for child Item Label
txtDynaListValue = "menu_child" // Name of recordset field for child Value
Set oDynaListRS = zpchildrs // Name of child list box recordset

Dim varDynaList
varDynaList = -1Dim varMaxWidth
varMaxWidth = "1"Dim varCheckGroup
varCheckGroup = oDynaListRS.Fields.Item(txtDynaListRelation).ValueDim varCheckLength
varCheckLength = 0Dim varMaxLength
varMaxLength = 0While (NOT oDynaListRS.EOF)If (varCheckGroup <> oDynaListRS.Fields.Item(txtDynaListRelation).Value) Then
If (varCheckLength > varMaxLength) Then
varMaxLength = varCheckLength
End If
varCheckLength = 0
End If
%>
arrDynaList[<%=(varDynaList+1)%>] = "<%=(oDynaListRS.Fields.Item(txtDynaListRelation).Value)%>"
arrDynaList[<%=(varDynaList+2)%>] = "<%=(oDynaListRS.Fields.Item(txtDynaListLabel).Value)%>"
arrDynaList[<%=(varDynaList+3)%>] = "<%=(oDynaListRS.Fields.Item(txtDynaListValue).Value)%>"
<%
If (len(oDynaListRS.Fields.Item(txtDynaListLabel).Value) > len(varMaxWidth)) Then
varMaxWidth = oDynaListRS.Fields.Item(txtDynaListLabel).Value
End If
varCheckLength = varCheckLength + 1
varDynaList = varDynaList + 3
oDynaListRS.MoveNext()
WendIf (varCheckLength > varMaxLength) Then
varMaxLength = varCheckLength
End If
%>//-->
</SCRIPT><SCRIPT language=JavaScript type=text/javascript>
<!--
function setDynaList(arrDL){var oList1 = document.forms[arrDL[2]].elements[arrDL[1]];
var oList2 = document.forms[arrDL[4]].elements[arrDL[3]];
var arrList = arrDL[5];

clearDynaList(oList2);

if (oList1.selectedIndex == -1){
oList1.selectedIndex = 0;
}populateDynaList(oList2, oList1[oList1.selectedIndex].value, arrList);
return true;
}

function clearDynaList(oList){for (var i = oList.options.length; i >= 0; i--){
oList.options[i] = null;
}

oList.selectedIndex = -1;
}

function populateDynaList(oList, nIndex, aArray){for (var i = 0; i < aArray.length; i= i + 3){
if (aArray[i] == nIndex){
oList.options[oList.options.length] = new Option(aArray[i + 1], aArray[i + 2]);
}
}if (oList.options.length == 0){
oList.options[oList.options.length] = new Option("[none available]",0);
}

oList.selectedIndex = 0;
}function Kermy_JS(jsStr) {
return eval(jsStr)
}function MM_jumpMenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
//-->
</SCRIPT>-------------------------------連動菜單結束-----------------------------------//以下為上傳界面==============================================<TABLE borderColor=#0099ff height=80 cellSpacing=1 cellPadding=0 width="100%" bgColor=#0099ff border=0>
<TBODY>
<TR>
<TD vAlign=center align=left width="15%" bgColor=#ffffff height=37>選擇類型:</TD>
<TD vAlign=center align=left bgColor=#ffffff colSpan=2>添加具體項目:<SELECT id=mainmenu onchange="Kermy_JS('setDynaList(arrDL1)')" name=mainmenu ?> <OPTION value="<%=(zpmenurs.Fields.Item(" selected menu_father?).Value)%>"></OPTION></SELECT><SELECT id=submenu name=submenu></SELECT><% '上傳文件個數
num=1

if request.querystring("number")<>"" Then

num=Trim(request.querystring("number"))end if
%>上傳文件數 <SELECT onchange="MM_jumpMenu('parent',this,0)" name=menu1> <OPTION selected>== 請選擇==</OPTION> <OPTION value=?number=1>1</OPTION> <OPTION value="?number=<%=i%>"></OPTION></SELECT> <INPUT id=num type=hidden value="<%=num %>" name=num></TD></TR><%
for j=1 to num
%>
<TR>
<TD vAlign=center align=left bgColor=#ffffff>上傳圖片<%= j %>:</TD>
<TD vAlign=center align=left bgColor=#ffffff colSpan=2><INPUT id=pic style="WIDTH: 100px" type=file onchange=null.value=no name=pic> </TD></TR><% next %>
<TR>
<TD vAlign=center align=left bgColor=#ffffff> </TD>
<TD vAlign=center align=left bgColor=#ffffff colSpan=2><INPUT type=submit value=提交 name=Submit> <LABEL><INPUT type=reset value=重置 name=Submit2> </LABEL></TD></TR>
<TR>
<TD height=1></TD>
<TD width="34%"></TD>
<TD width="51%"></TD></TR></TBODY></TABLE><%
zpmenurs.Close()
Set zpmenurs = Nothing
%><%
zpchildrs.Close()
Set zpchildrs = Nothing
%>up.asp 結束

『叄』 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" />

不好意思,未做注釋,有不懂的先到網上搜一下,不行再聯系我吧

『肆』 如何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上傳圖片的原理和示例都講完了,有什麼不妥的地方請各位指正,謝謝!

『伍』 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>

『陸』 急!!!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/*",
以便正常顯示圖片。

『柒』 ASP如何實現批量上傳圖片,並在資料庫中有記錄

<%

dim conn

dim rs

dim rs2


set conn=server.createobject("adodb.connection")

conn.connectionstring="Provider = Microsoft.Jet.OLEDB.4.0;Data Source="&server.mapPath("db.mdb")

conn.open


formsize=request.totalbytes


if formsize<>0 then '這里只判斷了是否等於0,等於0就說明沒有傳過來數據。以後可以要多做點判斷,比如限制圖片大小的時候要判斷一下。

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)'這里也要檢測一下,就是檢測這個數據裡面是不是有病毒詞彙之類的,這個可以在網上查一下病毒裡面可能出現的詞語,如果有這些詞語,直接在這里就把這個mydata銷毀,然後response.redirect "xxx.asp",有病毒趕快跳轉,不要處理帶病毒的數據。


set rs=server.createobject("ADODB.recordset")

sql="select * from imgurl"

rs.open sql,conn,1,3

rs.addnew

rs("img").AppendChunk myData'把這個圖片的二進制數據新增到img欄位裡面。

tpm=replace(now(),"/","_") '用的日期做名字,日期裡面有"/"," ",":"之類的東西,這些東西全部替換成"_"。

tpm=replace(tpm,":","_")

tpm=replace(tpm," ","_")

rs("圖片名")=tpm

rs.update


Set MyStream=Server.CreateObject("Adodb.Stream")

MyStream.Type=1

MyStream.Open

MyStream.Write rs("img").getChunk(8000000)

MyStream.SaveToFile server.mappath("pic/"&tpm&".jpg")

MyStream.close

set MyStream=nothing


rs("img")="" '把access裡面的二進制圖片內容刪除,只保留圖片的名字。

rs.update


rs.close

set rs=nothing


response.write "<script>alert(""上傳成功"");location.href=""upload.asp"";</script>"

end if

%>


<form action="upload.asp" method="post" enctype="multipart/form-data">

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

<input type="submit" name="ok" value="上傳">

</form>



<!--------------下面是把圖片顯示出來------------------->



<div style="margin-top:2em;"><!--------------創建一個div把圖片顯示區域定位一下------------------->

<%

set rs2=server.createobject("ADODB.recordset")

sql2="select * from imgurl order by id desc"

rs2.open sql2,conn,1,1

for s=1 to rs2.recordcount

if not rs2.eof and not rs2.bof then

%>

<img src="pic/<%=rs2("圖片名")%>.jpg">

<%

else

exit for

end if

rs2.movenext

next

rs2.close

set rs2=nothing

%>

</div>

<%

conn.Close

Set conn = Nothing

%>


<!--至於刪除圖片,這個就很簡單了,圖片的名字都已經進access裡面了,直接讀一下access裡面的名字,讀了一個名字,然後用fso在pic文件夾裡面去刪除對應的圖片文件就可以了。--->

效果圖:網頁鏈接

熱點內容
安卓平板生產力如何 發布:2025-02-07 13:07:20 瀏覽:349
維沃新系統和安卓系統有什麼區別 發布:2025-02-07 13:06:36 瀏覽:831
壓縮機過熱保護器在哪 發布:2025-02-07 13:03:21 瀏覽:42
安裝win8需要什麼配置 發布:2025-02-07 13:00:34 瀏覽:676
大板演算法 發布:2025-02-07 12:56:47 瀏覽:254
tplink路由器如何配置 發布:2025-02-07 12:50:48 瀏覽:428
unicode轉中文python 發布:2025-02-07 12:45:21 瀏覽:287
學習python用什麼軟體 發布:2025-02-07 12:45:15 瀏覽:611
怎麼看bin文件編譯日期 發布:2025-02-07 12:44:27 瀏覽:391
怎麼啟動ftp服務 發布:2025-02-07 12:27:46 瀏覽:865