当前位置:首页 » 文件管理 » 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 09:49:15 浏览:53
享宇钱包上传不了照片 发布:2025-02-07 09:48:32 浏览:632
模拟器怎么开安卓版本 发布:2025-02-07 09:42:35 浏览:771
c程序设计语言源码 发布:2025-02-07 09:22:41 浏览:623
数据库域的概念 发布:2025-02-07 09:22:40 浏览:640
电信路由器账号密码多少 发布:2025-02-07 09:22:01 浏览:671
我的世界怎么在别人服务器开光影 发布:2025-02-07 09:21:20 浏览:12
qq上传视频很慢怎么办 发布:2025-02-07 09:16:04 浏览:697
pythonredis缓存 发布:2025-02-07 09:10:24 浏览:928
封边机主要看哪些配置 发布:2025-02-07 09:10:17 浏览:906