ASP如何实现批量上传图片,并在数据库中有记录

ASP如何实现批量上传图片,并在数据库中有记录... ASP如何实现批量上传图片,并在数据库中有记录 展开
 我来答
小鸟蚁人
2020-03-27 · 超过13用户采纳过TA的回答
知道答主
回答量:124
采纳率:71%
帮助的人:8.4万
展开全部

<%

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文件夹里面去删除对应的图片文件就可以了。--->

效果图:网页链接

apple_86578899
推荐于2018-04-05 · 超过20用户采纳过TA的回答
知道答主
回答量:59
采纳率:0%
帮助的人:0
展开全部
上传界面:
<table width="100%" border="0" align="center" cellpadding="3" cellspacing="0" class="border">
<form name="form1" method="post" action="upfiles.asp" enctype="multipart/form-data">
<tr>
<td height="25" valign="middle" align="center" colspan="2" class="Title"><strong>批量上传产品图片</strong></td>
</tr>
<form name="form1" method="post" action="upfiles.asp" enctype="multipart/form-data">
<tr class=CommonListTitle height="25" bgcolor="#eeeeee">
<td height="22" align="center" valign="middle" class="tdbg"> 文件上传
<input type="hidden" name="act" value="upload">
(提交后请稍等!)</td>
</tr>
<tr c>
<td height="22" align="center" valign="middle" class="tdbg">
<script language="javascript">
function setid()
{
str='<br>';
if(!window.form1.upcount.value)
window.form1.upcount.value=1;
for(i=1;i<=window.form1.upcount.value;i++)
str+='文件'+i+':<input type="file" name="file'+i+'" style="width:250"><br>';
window.upid.innerHTML=str+'<br>';
}
</script>
<li> 上传个数:
<input name="upcount" type="text" value="1" size="10">
<input name="Button" type="button" class="button" onClick="setid();" value="· 设定 ·">

</li>
</td>
</tr>
<tr bgcolor="#eeeeee">
<td align="center" valign="middle" class="tdbg" id="upid"> 文件1:
<input type="file" name="file1" style="width:250" value=""></td>
</tr>
<tr bgcolor="#eeeeee">
<td height="22" align="center" valign="middle" class="tdbg">
<input name="Submit" type="submit" class="button" value="· 提交 ·">

<input name="Submit2" type="reset" class="button" value="· 重执 ·"></td>
</tr>
</form>
</table>

upfiles.asp 上传结果:
<%
dim conn,mypath
set conn=server.createobject("adodb.connection")
mypath=server.mappath("../../database/global.asa") '建立数据库连接.
conn.open "provider=microsoft.jet.oledb.4.0;data source=" & mypath
%>
<!--#include FILE="upload_5xsoft.inc"-->
<html>
<head>
<title>photo</title>
<link href="../style/style.css" rel="stylesheet" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<table align="center" cellpadding="4" cellspacing="1" class="toptable grid" border="1"><tr class=CommonListTitle height="25">
<td>上传结果</td>
</tr>
<tr class="CommonListCell">
<td>
<%
dim upload,file,formName,formPath,iCount,Photo_Beizhu,upcount
set upload=new upload_5xSoft ''建立上传对象

response.write upload.Version&"<br><br>" ''显示上传类的版本
formPath="../images/"

iCount=0
for each formName in upload.file ''列出所有上传了的文件
set file=upload.file(formName) ''生成一个文件对象
if file.FileSize>0 then ''如果 FileSize > 0 说明有文件数据
file.SaveAs Server.mappath(formPath&file.FileName) ''保存文件
'录入数据库
response.write file.FilePath&file.FileName&" ("&file.FileSize&") => "&formPath&File.FileName&" 成功!<br>"
iCount=iCount+1
end if
set file=nothing
next
set upload=nothing ''删除此对象
Htmend iCount&" 个文件上传成功!"

sub HtmEnd(Msg)
set upload=nothing
response.write "<br>"&Msg&" [<a href=""javascript:history.back();"">RETURN</a>]"
end sub
%></td>
</tr>
</table>

</body>
</html>

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="上传程序 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.copyto dr,FileSize
dr.SaveToFile FullPath,2
dr.Close
set dr=nothing
SaveAs=0
end function
End Class
</SCRIPT>
在upfiles.asp文件下加保存到数据库的代码就可以了
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式