图片批量上传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文件夹里面去删除对应的图片文件就可以了。--->
效果图:网页链接
Ⅱ 求ASP多张图片上传的详细代码
<%Server.ScriptTimeOut=5000%>
<!--#include file="conn.asp"-->
<!--#include file="upload_5xsoft.inc"-->
<html>
<head>
<title>文件上传</title>
<link rel="stylesheet" href="images/style.css" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<%
dim upload,file,formName,formPath,iCount,exeec,wjm,lm
set upload=new upload_5xsoft
formPath="pictures/"
wjm=upload.form("file")
wjdx=0
for each formName in upload.objFile
if upload.form("zclj")="" then
set file=upload.file(formName)
wjdx = file.filesize
kzm = right(file.filename,4)
else
kzm = right(wjm,4)
end if
if lcase(kzm)<>".gif" and lcase(kzm)<>".bmp" and lcase(kzm)<>".jpg" then response.Redirect("add_pic.asp")
if upload.form("zclj")<>"" or wjdx<819200 then
set rs=server.CreateObject("adodb.recordset")
name=formPath + replace(cstr(date()),"-","") + replace(cstr(time()),":","") + kzm
'定义记录集,把用户输入的数据插入到数据表中
sql="select * from message"
rs.open sql,conn,1,3
rs.addnew
rs("add_date")=right(cstr(date()),len(cstr(date()))-2)
rs("add_time")=time
if upload.form("zclj")="" then
rs("zp")=name
dx=cstr(file.filesize/1024)
if len(dx) >6 then dx=left(dx,6)
rs("wjdx") = dx +"K"
else
rs("zp")=wjm
rs("wjdx") = "0K"
end if
small_class_id=upload.form("small_class_id")
rs("small_class_id")=small_class_id
rs("big_class_id")=small_class_id
zt=upload.form("zt")
rs("zt")=zt
title=upload.form("title")
rs("title")=title
if wjdx>0 and wjdx<819200 and upload.form("zclj")="" then
file.SaveAs Server.mappath(name)
end if
iCount=iCount+1
rs.update
rs.close
cg="1"
end if
set file=nothing
next
set upload=nothing '删除此对象
sub HtmEnd(Msg)
set upload=nothing
response.end
end sub
if cg="1" then response.redirect "add_pic.asp"
%>
不能上传大于800K的文件(0.8M)!
</body>
</html>
=========
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#include file="images/style.css" -->
<body>
<form name="dd" method="post" action="scfile.asp" enctype="multipart/form-data" >
<input type="hidden" name="act" value="upload">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr><td>图片主题:<input name="zt" type="text" class="inputtext" size="30" maxlength="100"></td></tr>
<tr><td>标题:<input name="title" type="text" class="inputtext" id="title" size="65"></td></tr>
<tr>
<td colspan="2" align="left" id="upid2">
<label>
<select name="small_class_id" id="small_class_id">
<option value="52">第1-8类商品</option>
<option value="44">第9-14类商品 </option>
<option value="79">第15-19类商品</option>
<option value="124">第20-22类商品</option>
</select>
</label> </td>
</tr>
<tr height="25">
<td colspan="2" align="left" id="upid"><input type="file" name="file1"></td>
</tr>
<tr height="25">
<td colspan="2" align="left" id="upid"></td>
</tr>
<tr height="25">
<td colspan="2" align="center">
<input type="submit" value=" 发 送 " name="cmdOk" class="inputbox" style="font-family: 宋体; font-size: 9pt;">
<input type="reset" name="Submit2" value=" 重 设 " class="inputbox" style="font-family: 宋体; font-size: 9pt;"> </td>
</tr>
</table>
</form>
</body>
</html>
Ⅲ ASP无组件批量上传图片
使用风声无组件上传类
<%
Set myRequest=new UpLoadClass
myRequest.FileType ="gif/jpg"'"gif/jpg"
myRequest.Savepath = "pic/"
myrequest.open
sql = "select * from tab"
set rs = server.createobject("adodb.recordset")
rs.open sql,conn,1,3
rs.addNew
rs("title") = myRequest.Form("title")
rs("file1") = myRequest.Form("file1")
rs("file2") = myRequest.Form("file2")
rs.update
set rs = nothing
%>
1.文件自动上传到pic目录自按日期时间生成不重复的文件名
2.上传的文件的文件名自动生成并可这样取出
myRequest.Form("file1")
3.text框的数据这样取
myRequest.Form("title")
4.这个类的代码可在这里
<%
'*****************************图片上传类开始*******************
Class UpLoadClass
Private p_MaxSize,p_FileType,p_SavePath,p_AutoSave,p_Error
Private objForm,binForm,binItem,strDate,lngTime
Public FormItem,FileItem
Public Property Get Version
Version="Rumor UpLoadClass Version 2.0"
End Property
Public Property Get Error
Error=p_Error
End Property
Public Property Get MaxSize
MaxSize=p_MaxSize
End Property
Public Property Let MaxSize(lngSize)
if isNumeric(lngSize) then
p_MaxSize=clng(lngSize)
end if
End Property
Public Property Get FileType
FileType=p_FileType
End Property
Public Property Let FileType(strType)
p_FileType=strType
End Property
Public Property Get SavePath
SavePath=p_SavePath
End Property
Public Property Let SavePath(strPath)
p_SavePath=replace(strPath,chr(0),"")
End Property
Public Property Get AutoSave
AutoSave=p_AutoSave
End Property
Public Property Let AutoSave(byVal Flag)
select case Flag
case 0:
case 1:
case 2:
case false:Flag=2
case else:Flag=0
end select
p_AutoSave=Flag
End Property
Private Sub Class_Initialize
p_Error = -1
p_MaxSize = 204800000000'0//设定上传图片大小
p_FileType = "jpg/gif"
p_SavePath = ""
p_AutoSave = 0
strDate = replace(cstr(Date()),"-","")
strDate = replace(cstr(Date()),"/","")
lngTime = clng(timer()*1000)
Set binForm = Server.CreateObject("ADODB.Stream")
Set binItem = Server.CreateObject("ADODB.Stream")
Set objForm = Server.CreateObject("Scripting.Dictionary")
objForm.CompareMode = 1
End Sub
Private Sub Class_Terminate
objForm.RemoveAll
Set objForm = nothing
Set binItem = nothing
'binForm.Close()
Set binForm = nothing
End Sub
Public Sub Open()
if p_Error=-1 then
p_Error=0
else
Exit Sub
end if
Dim lngRequestSize,binRequestData,strFormItem,strFileItem
Const strSplit="'"">"
lngRequestSize=Request.TotalBytes
if lngRequestSize<1 then
p_Error=4
Exit Sub
end if
binRequestData=Request.BinaryRead(lngRequestSize)
binForm.Type = 1
binForm.Open
binForm.Write binRequestData
Dim bCrLf,strSeparator,intSeparator
bCrLf=ChrB(13)&ChrB(10)
intSeparator=InstrB(1,binRequestData,bCrLf)-1
strSeparator=LeftB(binRequestData,intSeparator)
Dim p_start,p_end,strItem,strInam,intTemp,strTemp
Dim strFtyp,strFnam,strFext,lngFsiz
p_start=intSeparator+2
Do
p_end =InStrB(p_start,binRequestData,bCrLf&bCrLf)+3
binItem.Type=1
binItem.Open
binForm.Position=p_start
binForm.CopyTo binItem,p_end-p_start
binItem.Position=0
binItem.Type=2
binItem.Charset="gb2312"
strItem=binItem.ReadText
binItem.Close()
p_start=p_end
p_end =InStrB(p_start,binRequestData,strSeparator)-1
binItem.Type=1
binItem.Open
binForm.Position=p_start
lngFsiz=p_end-p_start-2
binForm.CopyTo binItem,lngFsiz
intTemp=Instr(39,strItem,"""")
strInam=Mid(strItem,39,intTemp-39)
if Instr(intTemp,strItem,"filename=""")<>0 then
if not objForm.Exists(strInam&"_From") then
strFileItem=strFileItem&strSplit&strInam
if binItem.Size<>0 then
intTemp=intTemp+13
strFtyp=Mid(strItem,Instr(intTemp,strItem,"Content-Type: ")+14)
strTemp=Mid(strItem,intTemp,Instr(intTemp,strItem,"""")-intTemp)
intTemp=InstrRev(strTemp,"\")
strFnam=Mid(strTemp,intTemp+1)
objForm.Add strInam&"_Type",strFtyp
objForm.Add strInam&"_Name",strFnam
objForm.Add strInam&"_Path",Left(strTemp,intTemp)
objForm.Add strInam&"_Size",lngFsiz
if Instr(intTemp,strTemp,".")<>0 then
strFext=Mid(strTemp,InstrRev(strTemp,".")+1)
else
strFext=""
end if
if left(strFtyp,6)="image/" then
binItem.Position=0
binItem.Type=1
strTemp=binItem.read(10)
if strcomp(strTemp,chrb(255) & chrb(216) & chrb(255) & chrb(224) & chrb(0) & chrb(16) & chrb(74) & chrb(70) & chrb(73) & chrb(70),0)=0 then
if Lcase(strFext)<>"jpg" then strFext="jpg"
binItem.Position=3
do while not binItem.EOS
do
intTemp = ascb(binItem.Read(1))
loop while intTemp = 255 and not binItem.EOS
if intTemp < 192 or intTemp > 195 then
binItem.read(Bin2Val(binItem.Read(2))-2)
else
Exit do
end if
do
intTemp = ascb(binItem.Read(1))
loop while intTemp < 255 and not binItem.EOS
loop
binItem.Read(3)
objForm.Add strInam&"_Height",Bin2Val(binItem.Read(2))
objForm.Add strInam&"_Width",Bin2Val(binItem.Read(2))
elseif strcomp(leftB(strTemp,8),chrb(137) & chrb(80) & chrb(78) & chrb(71) & chrb(13) & chrb(10) & chrb(26) & chrb(10),0)=0 then
if Lcase(strFext)<>"png" then strFext="png"
binItem.Position=18
objForm.Add strInam&"_Width",Bin2Val(binItem.Read(2))
binItem.Read(2)
objForm.Add strInam&"_Height",Bin2Val(binItem.Read(2))
elseif strcomp(leftB(strTemp,6),chrb(71) & chrb(73) & chrb(70) & chrb(56) & chrb(57) & chrb(97),0)=0 or strcomp(leftB(strTemp,6),chrb(71) & chrb(73) & chrb(70) & chrb(56) & chrb(55) & chrb(97),0)=0 then
if Lcase(strFext)<>"gif" then strFext="gif"
binItem.Position=6
objForm.Add strInam&"_Width",BinVal2(binItem.Read(2))
objForm.Add strInam&"_Height",BinVal2(binItem.Read(2))
elseif strcomp(leftB(strTemp,2),chrb(66) & chrb(77),0)=0 then
if Lcase(strFext)<>"bmp" then strFext="bmp"
binItem.Position=18
objForm.Add strInam&"_Width",BinVal2(binItem.Read(4))
objForm.Add strInam&"_Height",BinVal2(binItem.Read(4))
end if
end if
objForm.Add strInam&"_Ext",strFext
objForm.Add strInam&"_From",p_start
intTemp=GetFerr(lngFsiz,strFext)
if p_AutoSave<>2 then
objForm.Add strInam&"_Err",intTemp
if intTemp=0 then
if p_AutoSave=0 then
strFnam=GetTimeStr()
if strFext<>"" then strFnam=strFnam&"."&strFext
end if
binItem.SaveToFile Server.MapPath(p_SavePath&strFnam),2
objForm.Add strInam,strFnam
end if
end if
else
objForm.Add strInam&"_Err",-1
end if
end if
else
binItem.Position=0
binItem.Type=2
binItem.Charset="gb2312"
strTemp=binItem.ReadText
if objForm.Exists(strInam) then
objForm(strInam) = objForm(strInam)&","&strTemp
else
strFormItem=strFormItem&strSplit&strInam
objForm.Add strInam,strTemp
end if
end if
binItem.Close()
p_start = p_end+intSeparator+2
loop Until p_start+3>lngRequestSize
FormItem=split(strFormItem,strSplit)
FileItem=split(strFileItem,strSplit)
End Sub
Private Function GetTimeStr()
lngTime=lngTime+1
GetTimeStr=strDate&lngTime
End Function
Private Function GetFerr(lngFsiz,strFext)
dim intFerr
intFerr=0
if lngFsiz>p_MaxSize and p_MaxSize>0 then
if p_Error=0 or p_Error=2 then p_Error=p_Error+1
intFerr=intFerr+1
end if
if Instr(1,LCase("/"&p_FileType&"/"),LCase("/"&strFext&"/"))=0 and p_FileType<>"" then
if p_Error<2 then p_Error=p_Error+2
intFerr=intFerr+2
end if
GetFerr=intFerr
End Function
Public Function Save(Item,strFnam)
Save=false
if objForm.Exists(Item&"_From") then
dim intFerr,strFext
strFext=objForm(Item&"_Ext")
intFerr=GetFerr(objForm(Item&"_Size"),strFext)
if objForm.Exists(Item&"_Err") then
if intFerr=0 then
objForm(Item&"_Err")=0
end if
else
objForm.Add Item&"_Err",intFerr
end if
if intFerr<>0 then Exit Function
if VarType(strFnam)=2 then
select case strFnam
case 0:strFnam=GetTimeStr()
if strFext<>"" then strFnam=strFnam&"."&strFext
case 1:strFnam=objForm(Item&"_Name")
end select
end if
binItem.Type = 1
binItem.Open
binForm.Position = objForm(Item&"_From")
binForm.CopyTo binItem,objForm(Item&"_Size")
binItem.SaveToFile Server.MapPath(p_SavePath&strFnam),2
binItem.Close()
if objForm.Exists(Item) then
objForm(Item)=strFnam
else
objForm.Add Item,strFnam
end if
Save=true
end if
End Function
Public Function GetData(Item)
GetData=""
if objForm.Exists(Item&"_From") then
if GetFerr(objForm(Item&"_Size"),objForm(Item&"_Ext"))<>0 then Exit Function
binForm.Position = objForm(Item&"_From")
GetData=binFormStream.Read(objForm(Item&"_Size"))
end if
End Function
Public Function Form(Item)
if objForm.Exists(Item) then
Form=objForm(Item)
else
Form=""
end if
End Function
Private Function BinVal2(bin)
dim lngValue,i
lngValue = 0
for i = lenb(bin) to 1 step -1
lngValue = lngValue *256 + ascb(midb(bin,i,1))
next
BinVal2=lngValue
End Function
Private Function Bin2Val(bin)
dim lngValue,i
lngValue = 0
for i = 1 to lenb(bin)
lngValue = lngValue *256 + ascb(midb(bin,i,1))
next
Bin2Val=lngValue
End Function
End Class
%>