1. 论坛系统升级为Xenforo,欢迎大家测试!
    排除公告

[请教]asp 如何限制上传文件类型?

本帖由 風中墜落2005-12-30 发布。版面名称:后端开发

  1. 風中墜落

    風中墜落 New Member

    注册:
    2005-09-07
    帖子:
    20
    赞:
    0
  2. 風中墜落

    風中墜落 New Member

    注册:
    2005-09-07
    帖子:
    20
    赞:
    0
    下面这个是上传代码,比如我要将上传的文件类型设定为 JPG 要在哪里加入检测

    <html>
    <head>
    <title>图片上传</title>
    <style>
    td{font-size:9pt;line-height:140%}
    body{font-size:9pt;line-height:140%}
    a:link { color: #0033CC; text-decoration: none }
    a:visited { color: #0033CC; text-decoration: none }
    a:hover { color: #000000; text-decoration: underline}
    </style>
    </head>
    <body topmargin="0" leftmargin="0">
    <%
    dim upload,file,formName,formPath,iCount
    set upload=new upload_5xSoft ''建立上传对象

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

    if upload.form("filepath")="" then ''得到上传目录
    HtmEnd "请输入要上传至的目录!"
    set upload=nothing
    response.end
    else
    formPath=upload.form("filepath")
    ''在目录后加(/)
    if right(formPath,1)<>"/" then formPath=formPath&"/"
    end if

    iCount=0
    for each formName in upload.file ''列出所有上传了的文件
    set file=upload.file(formName) ''生成一个文件对象
    if file.FileSize>0 then ''如果 FileSize > 0 说明有文件数据
    dim fname
    fname = now()
    fname = replace(fname,"-","")
    fname = replace(fname," ","")
    fname = replace(fname,":","")
    fname = replace(fname,"PM","")
    fname = replace(fname,"AM","")
    fname = replace(fname,"上午","")
    fname = replace(fname,"下午","")
    fname = int(fname) + int((10-1+1)*Rnd + 1)
    'fname=""&fname&"."&GetExtendName(file.FileName)&""
    fname=""&fname&".gif"
    file.SaveAs Server.mappath(formPath&fname) ''保存文件
    tmp = "http://" & request.servervariables("SERVER_NAME") & _
    left(request.servervariables("SCRIPT_NAME"),len(request.servervariables("SCRIPT_NAME"))-len("/admin/upload.asp"))
    picurl=tmp&formPath&fname
    picurl = replace(picurl,"..","")
    end if
    set file=nothing
    next
    set upload=nothing
    'Htmend iCount&" 个文件上传结束!"
    sub HtmEnd(Msg)
    set upload=nothing
    end sub

    function GetExtendName(FileName)
    dim ExtName
    ExtName = LCase(FileName)
    ExtName = right(ExtName,3)
    ExtName = right(ExtName,3-Instr(ExtName,"."))
    GetExtendName = ExtName
    end function
    if picurl<>"" then%>
    请将图片地址复制到上面:<input type=text name=T1 size=50 value=<%=picurl%>> [<a href="Javascript:history.go(-1)">继续上传</a>]
    <%else%>请先选择你要上传的图片 [<a href="Javascript:history.go(-1)">重新上传</a>]<%end if%>
    </body>
    </html>
     
  3. 风

    New Member

    注册:
    2005-09-05
    帖子:
    145
    赞:
    0
    GetExtendName(file.FileName)

    这个函数返回的就是文件的扩展名,你可以将其与允许的扩展名加以比较,例如:

    代码:
    If GetExtendName(file.FileName) <> "jpg" And GetExtendName(file.FileName) <> "gif" Then
        Response.Write("不允许的文件")
        Response.End()
    End If
    你可以根据你的需要来完善该代码。
     
  4. 風中墜落

    風中墜落 New Member

    注册:
    2005-09-07
    帖子:
    20
    赞:
    0
    先谢谢楼上的,我去试试,
     
  5. 風中墜落

    風中墜落 New Member

    注册:
    2005-09-07
    帖子:
    20
    赞:
    0
    测试可以限制,但是不知道为什么不能上传格式为 torrent的文件

    我在比较那里已经设定了 但是点上传提示我不允许上传文件
     
  6. 風中墜落

    風中墜落 New Member

    注册:
    2005-09-07
    帖子:
    20
    赞:
    0
    已经解决,再次感谢楼上的
     
  7. 小叶

    小叶 New Member

    注册:
    2005-09-04
    帖子:
    17,941
    赞:
    33
    楼上提供的方法有漏洞的..嘎嘎..大小写....还有空格...
     
  8. 風中墜落

    風中墜落 New Member

    注册:
    2005-09-07
    帖子:
    20
    赞:
    0
    那应该怎么搞?
     
  9. 小叶

    小叶 New Member

    注册:
    2005-09-04
    帖子:
    17,941
    赞:
    33
    上传是一个非常大的问题,到现在也没好好解决,
    看动网每次被黑就知道上传的麻烦