回 帖 发 新 帖 刷新版面

主题:关于上传文件的问题

[size=3][size=4][size=2]各位大哥大姐,小弟我碰到一个问题,在网上找一个无组件上传图片的代码,可以正常使用,可是他上传的图片名称是以时间做为名称,比如你上传一个图片名称本来是123.gif可是上传后会改成20094295643544.gif这样一个,可是现在我想我上传的图片名称改成我指成的名称。比如123.gif 上传后变成了 adtu1.gif 我改来改去都不行。大家帮我看一下,以下是我的代码。[/size][/size][/size]


======================代码================


<html>
<head>
<title>文件上传</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<script>
parent.document.forms[0].Submit.disabled=false;
</script>
<%
dim upload,file,formName,formPath,iCount,filename,fileExt
set upload=new upload_5xSoft ''建立上传对象
 formPath=upload.form("filepath")
 ''在目录后加(/)
 if right(formPath,1)<>"/" then formPath=formPath&"/" 
response.write "<body>"
iCount=0
for each formName in upload.file ''列出所有上传了的文件
 set file=upload.file(formName)  ''生成一个文件对象
 if file.filesize<100 then
     response.write "请选择你要上传的文件 [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
    response.end
 end if
     
 if file.filesize>2000*100 then
     response.write "文件大小超过了限制200K [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
    response.end
 end if
 fileExt=lcase(right(file.filename,4))
 uploadsuc=false
Forum_upload="gif,jpg,png"
 Forumupload=split(Forum_upload,",")
 for i=0 to ubound(Forumupload)
    if fileEXT="."&trim(Forumupload(i)) then
    uploadsuc=true
    exit for
    else
    uploadsuc=false
    end if
 next
 if uploadsuc=false then
     response.write "文件格式不正确 [ <a href=# onclick=history.go(-1)>重新上传</a> ]"
    response.end
 end if
 randomize
 ranNum=int(90000*rnd)+10000
 [color=FF0000]filename=formPath&year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&ranNum&fileExt 我知道是这句代码使文件名变成了时间,可是就是改不好。[/color]
 if file.FileSize>0 then         ''如果 FileSize > 0 说明有文件数据
  file.SaveAs Server.mappath(FileName)   ''保存文件
    for i=0 to ubound(Forumupload)
        if fileEXT="."&trim(Forumupload(i)) then
         response.write "<script>parent.form1.mdpic.value+='"&FileName&"'</script>"
        exit for
        end if
    next
 iCount=iCount+1
 end if
 set file=nothing
next
set upload=nothing  ''删除此对象
Htmend iCount&" 个文件上传结束!"
sub HtmEnd(Msg)
 set upload=nothing
 response.write "完成图片上传"
 response.end
end sub
%>
</body>
</html>

回复列表 (共1个回复)

沙发

改成时间作为文件名是为了防止文件重名覆盖.要作成自己的名字,就需要把原文件名读一下,取出.前面的名称.如abc.jpg,要把abc分离出来,如filenamenew="abc",当然abc是变量.然后你红字的部分写成
 filename=filenamenew&fileExt 如果通过输入名称来定义文件名, 则filenamenew应该是你输入的值.

我来回复

您尚未登录,请登录后再回复。点此登录或注册