求ASP上传图片代码(上传图片到指定文件夹,同时获取路径保存到数据库中)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了求ASP上传图片代码(上传图片到指定文件夹,同时获取路径保存到数据库中)相关的知识,希望对你有一定的参考价值。

上传图片到指定文件夹,同时获取路径保存到数据库中

要求不用组件的,代码清晰

上传界面:
<table border="0" align="center" cellpadding="3" cellspacing="0" class="border">
<form name="form1" method="post" action="upfiles.asp" enctype="multipart/form-data">
<tr>
<td 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 bgcolor="#eeeeee">
<td align="center" valign="middle" class="tdbg"> 文件上传
<input type="hidden" name="act" value="upload">
(提交后请稍等!)</td>
</tr>
<tr c>
<td 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 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 >
<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文件下加保存到数据库的代码就可以了
参考技术A 哈哈,希望我的这个能对你有所帮助
<%
Dim name:name="图文混排添加文章"
response.write MYhead("易博智能建站管理后台-"& name &"","left")
response.write ebcssjd(name)
Dim lid:lid=usb(request("lid"))
Dim lx:lx=usb(request("lx"))
If lx="" Then lx=2
Dim ShangChuan_GeShi_H:ShangChuan_GeShi_H="asp,asa,inc,aspx,php,jsp,htm,html"
Dim BiaoDanShu:BiaoDanShu=99
Dim rjjsb:rjjsb=RndNumber(1,999999999)
if lx="2" then lxtxt="文章"
Function UTF8(Str)
Dim i,OneStr,AllStr
For i = 1 To Len(Str)
OneStr = Mid(Str,i,1)
AllStr = AllStr & chr(38) & chr(35) & chr(120) & Hex(Ascw(OneStr)) & chr(59)
Next
UTF8 = AllStr
End Function

function deletefile(filename)
if filename<>"" then
filename=server.MapPath(""&fileName&"")
set fso=server.CreateObject("scripting.filesystemobject")
if fso.FileExists(filename) then
fso.DeleteFile filename
else
'Response.Write "该文件不存在<br/>"
end if
end if
end function
rsformPath="/test/"'文件存放的目录
rsnamekind="2"'是否自动命名
rsfilesize="2048000000"'上传文件的大小
'rsallowedfile="gif,jpg,png,jpeg,MBM,BMP"'上传文件的白名单
rsallowedfile=lx_upfile
If Right(rsformPath,1)<>"\" Then rsformPath=rsformPath&"\"

dim upload,file,formName,formPath,iCount
Dim sjs,fname,i,l,newfilelist,uploaddir,filename,ii
''--------------------------
Server.ScriptTimeOut=999999
set upload=new upload_5xsoft
''---------------------------
fullpath=Server.Mappath("\")&"\"
addip=request.serverVariables("remote_host")
pass=upload.form("pass")
name=upload.form("name")
pagenext=upload.form("pagenext")
sf=upload.form("sf")
sffs=upload.form("sffs")
idpp=upload.form("idpp")
ulx=upload.form("ulx")
shu=upload.Form("shu")
addid=session("id")
''--------------------------
On Error Resume Next
iCount=0
errsl=0
If CLng(shu) > CLng(BiaoDanShu) Then
cw="一次最多只能提交" & BiaoDanShu & "个文件<br/>"
Else
for each formName in upload.objFile '
set file=upload.file(formName) ''
size=file.filesize
''-------------------------
If size>rsfilesize*1024 Then cw=cw&"超过限制大小<br/>"
If size=0 Then
errsl=errsl+1
'Exit For
End if
size=Round(size/1024,2)&" KB"
If Left(size,1)="." Then size="0"&size

''-------------------------
filetype=file.filename
i=InStrRev(filetype,".")
l=Len(filetype)
If i>0 Then
filetype=Right(filetype,l-i)
End If

'处理文件存放路径
if filetype="jpg" or filetype="gif" or filetype="bmp" or filetype="jpeg" or filetype="peg" or filetype="png" or filetype="jpge" then
newfile = rsformPath&"pic/" '图片
elseif filetype="mid" or filetype="midi" or filetype="mmf" or filetype="amr" or filetype="imy" or filetype="adp" or filetype="m4a" then
newfile = rsformPath&"bell/" '铃声
elseif filetype="sis" or filetype="sisx" or filetype="jar" or filetype="jad" or filetype="cab" or filetype="mpkg" then
newfile = rsformPath&"soft/" '安装软件
elseif filetype="umd" or filetype="chm" or filetype="txt" or filetype="brm" or filetype="prc" or filetype="tcr" or filetype="pdb" or filetype="mdx" then
newfile = rsformPath&"book/" '电子书
elseif filetype="bba" or filetype="smc" or filetype="nes" or filetype="nec" or filetype="gb" or filetype="gbc" or filetype="gba" or filetype="fba" or filetype="md" or filetype="sfc" then
newfile = rsformPath&"game/" '游戏
elseif filetype="thm" or filetype="hme" or filetype="ski" or filetype="nth" or filetype="utz" or filetype="mtf" or filetype="sdt" or filetype="tsk" or filetype="uiq" then
newfile = rsformPath&"sub/" '主题
elseif filetype="mp3" or filetype="aac" or filetype="wav" or filetype="ogg" or filetype="wma" then
newfile = rsformPath&"music/" '音乐
elseif filetype="mp4" or filetype="video" or filetype="wmv" or filetype="dat" or filetype="mpg" or filetype="mpeg4" or filetype="rm" or filetype="rmvb" or filetype="asf" or filetype="3gp" or filetype="avi" then
newfile = rsformPath&"mtv/" '视频
elseif filetype="lrc" or filetype="nol" then
newfile = rsformPath&"lrc/" '歌词,网络标志
elseif filetype="wml" then
newfile = rsformPath&"wml/" 'WML文件
else
newfile = rsformPath&"other/" '其他,rar,zip,pdf,pdg,emz等
end if

''-------------------------
filetype=LCase(filetype)
filetype=Replace(filetype,".","")
filetype=CStr(filetype)
If InStr(rsallowedfile,filetype)=0 Then cw=cw&"不允许的格式<br/>"'errtxt=false:Exit For
'If errtxt=False then
'cw="不允许的格式"
'End if
'----------------
If rsnamekind=1 Then
fname=file.filename
Else
Randomize
sjs=INT((99-00+1)*RND+00)
strMonth=month(now)
if len(strMonth)=1 then
strMonth="0"&strMonth
end if
strDay=day(now)
if len(strDay)=1 then
strDay="0"&strDay
end If
filename=newfile&year(now)&strMonth&strDay&lx_spot&hour(now)&minute(now)&second(now)&sjs&"."&filetype
'fname=year(date)&month(date)&day(date)&hour(time())&minute(time())&second(time())&sjs
fname=fullpath&filename
End If
''--------------------------
'处理文件存放路径,文件夹处理
if file.filesize>0 and cw="" Then
dim realpath
realpath=getRealPath(fjusb(filename))
'建立文件夹

setCreateFolder fullpath&realpath,filetype

file.SaveAs fullpath&realpath
if filetype="jpg" or filetype="bmp" or filetype="gif" or filetype="jpeg" or filetype="peg" or filetype="png" or filetype="jpge" Then
If lx_picsh="yes" Then
response.write CreatePic(fjusb(realpath),98)'图片缩放
response.write CreatePic(fjusb(realpath),176)
End If
End if
End If
datadir=realpath
addtime=date()
myname=fname
explain=explain
datadir=datadir
filesize=size
addid=addid
if cw="" then
set rs=Server.CreateObject("ADODB.Recordset")
rspl="select * from [rjfj]"
rs.open rspl,conn,1,2
rs.addnew
rs("rjsb")=rjjsb
'if id<>"" then rs("rjid")=id
if file.filename<>"" then rs("rjtxt")=file.filename
if filetype<>"" then rs("rjks")=filetype
if datadir<>"" then rs("rjurl")=fjusb(datadir)
if size<>"" then rs("rjsize")=size
if lid<>"" then rs("tid")=lid
'if id<>"" then rs("glid")=id
if myid<>"" then rs("fyid")=myid
rs.update
rs.close
set rs=Nothing
End if
iCount=iCount+1
msg=iCount&"个文件上传成功!<br/>"
If errsl<>"0" Then
cw=cw&errsl&"个文件上传失败!<br/>"
End If
If fjusb(datadir)<>"" Then newtxt=newtxt&"[img]"&fjusb(datadir)&"[/img][br]"
If upload.form("txt"&iCount&"")<>"" then newtxt=newtxt&usb(upload.form("txt"&iCount&""))&"[br]"
If pagenext="1" And CStr(iCount)<>CStr(shu) Then newtxt=newtxt&"[next]"
Next
End if
set upload=nothing
If name<>"" And newtxt<>"" Then
fname=getRealnewPath("view")&rjjsb&makefilename(now())
Response.write viewadd2(fname,newtxt)'判断并生成文章
txt=fname
'记录入库
set rsez=server.createobject("ADODB.Recordset")
aqlz="select * from [myrj]"
rsez.open aqlz,conn,1,3
rsez.addnew
rsez("lx")=lx
rsez("rjsb")=rjjsb
rsez("classid")=idpp
rsez("sf")=sf
rsez("sffs")=sffs
rsez("rjtxt")=txt
rsez("pass")=pass
if ulx<>"" then rsez("ulx")=ulx
rsez("rjname")=name
rsez("myid")=myid
rsez.update
msg=msg&"文章添加成功!请勿刷新,请返回!"
rsez.close
set rsez=Nothing
set rsez=server.createobject("ADODB.Recordset")
aqlz="select id,rjsb from ["&lx_sql&"_myrj] where rjsb="& rjjsb
rsez.open aqlz,conn,1,3
If Not rsez.eof Then
rjzyid=rsez(0)
End if
rsez.close
set rsez=nothing
set rsez=server.createobject("ADODB.Recordset")
aqlz="select rjid,glid,rjsb from ["&lx_sql&"_rjfj] where rjsb="& rjjsb
rsez.open aqlz,conn,1,3
If Not rsez.eof Then
rsez(0)=rjzyid
rsez(1)=rjzyid
rsez.update()
End if
rsez.close
set rsez=nothing

End If
response.write msg&("<br />")
response.write cw&("<br />")
response.write "------------<br />"
%>
参考技术B 上传文件类型及大小自定,自定上传文件夹,如果文件夹不存在则自动建立,如果文件名重复自动替换,上传完成显示文件路劲,这样的上传文件有现成的,但是要保存到数据库需要手动完成了,用得上的话加百度hi发给你。

ASP.NET利用.FileUpload上传图片并将图片名称保存到数据库,我要具体的代码

参考技术A 放一个fileupload控件,另外放一个button按钮,控制上传,在button的click事件中写:

if(fileupload.hasfile) //判断是否存在文件

string FilePath = Server.MapPath("/UploadFile/"); //获取图像存的路径
string Extension = Path.GetExtension(fileupload.PostedFile.FileName); //获取文件的类型
if(extension != ".jpg") //判断是不是jpg格式,需要其他格式多加几个或者用字符处理方式都行

return ;

string NewFilePath = DateTime.Now.ToString("yyyyMMddHHmmss") + Extension; //重命名图片,也可以用图片原有的名字
fileupload.SaveAs(FilePath + NewFilePath); //图片存到对应路径
string ImgPath = "/UploadFile/" + NewFilePath; //获取上传好的图片的路径
//将字符串存入数据库对应的字段即可

追问

写的很好,有没有可以判断图片上传不能超过多少MB的?

追答

在webconfig里面可以设置全局的大小,字节的话,你用限制的大小换算一下

不加这个得话,默认是4M
另外也可以再代码里面判断
HttpPostedFile upFile = fileupload.PostedFile;
int fileLength = upFile.ContentLength;
但是大小不能超过在webconfig里面设置的

追问

原来是大神,遇到不懂的还可以问你么?

追答

算不上大神,一起学习,有不懂的可以一起探讨探讨

本回答被提问者采纳

以上是关于求ASP上传图片代码(上传图片到指定文件夹,同时获取路径保存到数据库中)的主要内容,如果未能解决你的问题,请参考以下文章

php表单上传图片到七牛云存储并返回地址……求具体流程~有代码更好

在ASP.NET中怎么把图片上传,并且存入数据库,求大神解答!!!!

asp.net上传图片到服务器路径的问题?

Ueditor百度编辑器 asp版本的 图片无法上传,提示网络连接错误!求帮助!

求PHP多图上传代码

android中如何上传图片到FTP服务器