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

2024-11-24 07:18:49
推荐回答(3个)
回答1:

上传界面:




















批量上传产品图片
文件上传

(提交后请稍等!)


  • 上传个数:




  • 文件1:





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



    photo










    上传结果

    <%
    dim upload,file,formName,formPath,iCount,Photo_Beizhu,upcount
    set upload=new upload_5xSoft ''建立上传对象

    response.write upload.Version&"

    " ''显示上传类的版本
    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&" 成功!
    "
    iCount=iCount+1
    end if
    set file=nothing
    next
    set upload=nothing ''删除此对象
    Htmend iCount&" 个文件上传成功!"

    sub HtmEnd(Msg)
    set upload=nothing
    response.write "
    "&Msg&" [RETURN]"
    end sub
    %>





    upload_5xsoft.inc文件:

    在upfiles.asp文件下加保存到数据库的代码就可以了

    回答2:

    哈哈,希望我的这个能对你有所帮助
    <%
    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 "该文件不存在
    "
    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 & "个文件
    "
    Else
    for each formName in upload.objFile '
    set file=upload.file(formName) ''
    size=file.filesize
    ''-------------------------
    If size>rsfilesize*1024 Then cw=cw&"超过限制大小
    "
    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&"不允许的格式
    "'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&"个文件上传成功!
    "
    If errsl<>"0" Then
    cw=cw&errsl&"个文件上传失败!
    "
    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&("
    ")
    response.write cw&("
    ")
    response.write "------------
    "
    %>

    回答3:

    上传文件类型及大小自定,自定上传文件夹,如果文件夹不存在则自动建立,如果文件名重复自动替换,上传完成显示文件路劲,这样的上传文件有现成的,但是要保存到数据库需要手动完成了,用得上的话加百度hi发给你。