| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 858 人关注过本帖
标题:上传附件
只看楼主 加入收藏
ljjlxj
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-3-27
结帖率:0
收藏
已结贴  问题点数:20 回复次数:9 
上传附件
上传附件时文件名为什么不是附件的名称,而是随机生成的数字。
在网上找的代码,刚接触asp,菜鸟一个,帮看看在哪里改代码

<!--#include file="Conn.asp"-->
<!--#include file="Function.asp"-->
<!--#include file="upfile_class.asp"-->
<%Server.ScriptTimeOut=999999999%>
<style type="text/css">
body{
margin:0px;
font-family:"verdana";
font-size:12px;
}
table,td{font-family:"verdana";
font-size:12px;}
A:link,A:active,A:visited
{TEXT-DECORATION:none ;Color:#000000
}
INPUT {
    BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; FONT-SIZE: 12px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px
}
</style>
<%
if Session("UserName")="" Then
response.write "您还没有登陆!"
response.End
end if
dim action
dim mySavePath,myformName
Dim upfilemaxsize,upfileallType
'重新设置
dim GroupInfo
GroupInfo=GetGroupInfo(Session("GroupID"))
upfilemaxsize=clng(GroupInfo(2))  '限制文件大小 KB
upfileallType=GroupInfo(1) '允许上传类型

action=trim(request("action"))
mySavePath=Request.QueryString("SavePath")
myformName=Request.QueryString("formName")
select case action
case "add"
add
case "save"
save
case else
add
end select

sub add
%>
<SCRIPT language=javascript>
function check()
{
    var strFileName=document.form1.FileName.value;
    if (strFileName=="")
    {
        alert("请选择要上传的文件。");
        document.form1.FileName.focus();
        return false;
      }
}
</SCRIPT>
<table>
<form action="?action=save&formName=<%=myformName%>&SavePath=<%=mySavePath%>" method="post" name="form1" onSubmit="return check()" enctype="multipart/form-data">
<tr><td>文件上传:<input name="FileName" type="file" id="FileName" size="40" >
<input type="submit" value="上传" name="submit" ><!--&nbsp;<font color="#FF0000"><%=CStr(upfilemaxsize)%>K</font>内-->
</form>
</td></tr>
</table>
<%
end sub
sub save
if GroupInfo(1)="" or GroupInfo(2)="" then
Response.Write("<script language=JavaScript>alert('您不允许上传文件,需要上传请与管理员联系!');document.location.href='javascript:window.history.go(-1);';</script>")
Response.End
End If
call upload_0()  '使用无组件上传类
end sub
sub upload_0()    '使用无组件上传类
dim upload,oFile,formName,SavePath,filename,fileExt,oFileSize
dim i,myfileName
dim msg,FoundErr
msg=""
FoundErr=false

    set upload=new upfile_class ''建立上传对象
    upload.GetData(104857600)   '取得上传数据,限制最大上传100M
    if upload.err > 0 then  '如果出错
        select case upload.err
            case 1
                response.write "请先选择你要上传的文件!"
            case 2
                response.write "你上传的文件总大小超出了最大限制(100M)"
        end select
        response.end
    end if


    SavePath ="UploadFile/"   '存放上传文件的目录
    if right(SavePath,1)<>"/" then SavePath=SavePath&"/" '在目录后加(/)
   
    'SavePath=CreatePath(SavePath)

    if right(SavePath,1)<>"/" then SavePath=SavePath&"/" '在目录后加(/)
   
    for each formName in upload.file '列出所有上传了的文件
        set ofile=upload.file(formName)  '生成一个文件对象
        oFileSize=ofile.filesize
        if oFileSize<100 then
            msg="请先选择你要上传的文件!"
            FoundErr=True
        elseif oFileSize>(upfilemaxsize*1024*1024) then
             msg="文件大小超过了限制,最大只能上传" & CStr(upfilemaxsize) & "MB的文件!"
            FoundErr=true
        end if

        fileExt=lcase(ofile.FileExt)
        If CheckFileExt(fileExt) = False then
            msg="上传失败!只允许上传这几种文件类型:" & replace(upfileallType,"|",",")
            FoundErr=true
        end if
        
        if FoundErr<>true then

            filename=FormatName(fileExt)
            myfileName=filename
            filename=SavePath&filename

            ofile.SaveToFile Server.mappath(FileName)   '保存文件
            
            Session("filesize")=Session("filesize")+oFileSize
            response.write "文件上传成功!文件大小为:" &FormatFileSize(oFileSize)&""
            response.write" <a href='?action=add&formName="&myformName&"&SavePath="&mySavePath&"'><font color=red>继续上传</font></a>"
Response.Write "<script>parent.AddItem('" &myfileName& "');</script>"
        else
            response.write("<font color=red>" & msg & "</font>&nbsp;<b><a href='javascript:history.back()'><font color=blue>返回<font></a></b>")
        end if
        
        set ofile=nothing
    next
    set upload=nothing
end sub


Function CheckFileExt(FileExt)
        Dim Forumupload,i
        CheckFileExt=False
        If FileExt="" or IsEmpty(FileExt) Then
            CheckFileExt = False
            Exit Function
        End If
        If FileExt="asp" or FileExt="asa" or FileExt="aspx" or FileExt="cer" or FileExt="cdx" then
            CheckFileExt = False
            Exit Function
        End If
        Forumupload = Split(LCase(upfileallType),"|")
        For i = 0 To ubound(Forumupload)
            If FileExt = Trim(Forumupload(i)) Then
                CheckFileExt = True
                Exit Function
            Else
                CheckFileExt = False
            End If
        Next
End Function
Function FormatName(Byval FileExt)
        Dim RanNum,TempStr
        Randomize
        RanNum = Int(90000*rnd)+10000
        TempStr = Year(now) & Month(now) & Day(now) & Hour(now) & Minute(now) & Second(now) & RanNum & "." & FileExt
        FormatName = TempStr
End Function

function FormatFileSize(xxx)
dim show11
show11=xxx & " Byte"
if xxx>=1024 then
   xxx=(xxx/1024)
   show11=round(xxx,2) & " KB"
end if
if xxx>=1024 then
   xxx=(xxx/1024)
   show11=round(xxx,2) & " MB"
end if
FormatFileSize=cstr(show11)
end function

Function CreatePath(PathValue)
    Dim objFSO,Fsofolder,uploadpath
        uploadpath = Year(Date) & "-" & Month(Date)
    If Right(PathValue,1)<>"/" Then PathValue = PathValue&"/"
    On Error Resume Next
    Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
    If objFSO.FolderExists(Server.MapPath(PathValue & uploadpath))=False Then
        objFSO.CreateFolder Server.MapPath(PathValue & uploadpath)
    End If
    If Err.Number = 0 Then
        CreatePath = PathValue & uploadpath & "/"
    Else
        CreatePath = PathValue
    End If
    Set objFSO = Nothing
End Function
%>

[ 本帖最后由 ljjlxj 于 2013-3-28 08:49 编辑 ]
搜索更多相关主题的帖子: include active style file 网上 
2013-03-27 17:23
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:5 
你是指 本坛子  还是你自己开发的

DO IT YOURSELF !
2013-03-27 19:32
ljjlxj
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-3-27
收藏
得分:0 
回复 2楼 wp231957
上传附件时文件名为什么不是附件的名称,而是随机生成的数字。
在网上找的代码,刚接触asp,菜鸟一个,帮看看在哪里改代码

<!--#include file="Conn.asp"-->
<!--#include file="Function.asp"-->
<!--#include file="upfile_class.asp"-->
<%Server.ScriptTimeOut=999999999%>
<style type="text/css">
body{
margin:0px;
font-family:"verdana";
font-size:12px;
}
table,td{font-family:"verdana";
font-size:12px;}
A:link,A:active,A:visited
{TEXT-DECORATION:none ;Color:#000000
}
INPUT {
    BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; FONT-SIZE: 12px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px
}
</style>
<%
if Session("UserName")="" Then
response.write "您还没有登陆!"
response.End
end if
dim action
dim mySavePath,myformName
Dim upfilemaxsize,upfileallType
'重新设置
dim GroupInfo
GroupInfo=GetGroupInfo(Session("GroupID"))
upfilemaxsize=clng(GroupInfo(2))  '限制文件大小 KB
upfileallType=GroupInfo(1) '允许上传类型

action=trim(request("action"))
mySavePath=Request.QueryString("SavePath")
myformName=Request.QueryString("formName")
select case action
case "add"
add
case "save"
save
case else
add
end select

sub add
%>
<SCRIPT language=javascript>
function check()
{
    var strFileName=document.form1.FileName.value;
    if (strFileName=="")
    {
        alert("请选择要上传的文件。");
        document.form1.FileName.focus();
        return false;
      }
}
</SCRIPT>
<table>
<form action="?action=save&formName=<%=myformName%>&SavePath=<%=mySavePath%>" method="post" name="form1" onSubmit="return check()" enctype="multipart/form-data">
<tr><td>文件上传:<input name="FileName" type="file" id="FileName" size="40" >
<input type="submit" value="上传" name="submit" ><!--&nbsp;<font color="#FF0000"><%=CStr(upfilemaxsize)%>K</font>内-->
</form>
</td></tr>
</table>
<%
end sub
sub save
if GroupInfo(1)="" or GroupInfo(2)="" then
Response.Write("<script language=JavaScript>alert('您不允许上传文件,需要上传请与管理员联系!');document.location.href='javascript:window.history.go(-1);';</script>")
Response.End
End If
call upload_0()  '使用无组件上传类
end sub
sub upload_0()    '使用无组件上传类
dim upload,oFile,formName,SavePath,filename,fileExt,oFileSize
dim i,myfileName
dim msg,FoundErr
msg=""
FoundErr=false

    set upload=new upfile_class ''建立上传对象
    upload.GetData(104857600)   '取得上传数据,限制最大上传100M
    if upload.err > 0 then  '如果出错
        select case upload.err
            case 1
                response.write "请先选择你要上传的文件!"
            case 2
                response.write "你上传的文件总大小超出了最大限制(100M)"
        end select
        response.end
    end if


    SavePath ="UploadFile/"   '存放上传文件的目录
    if right(SavePath,1)<>"/" then SavePath=SavePath&"/" '在目录后加(/)
   
    'SavePath=CreatePath(SavePath)

    if right(SavePath,1)<>"/" then SavePath=SavePath&"/" '在目录后加(/)
   
    for each formName in upload.file '列出所有上传了的文件
        set ofile=upload.file(formName)  '生成一个文件对象
        oFileSize=ofile.filesize
        if oFileSize<100 then
            msg="请先选择你要上传的文件!"
            FoundErr=True
        elseif oFileSize>(upfilemaxsize*1024*1024) then
             msg="文件大小超过了限制,最大只能上传" & CStr(upfilemaxsize) & "MB的文件!"
            FoundErr=true
        end if

        fileExt=lcase(ofile.FileExt)
        If CheckFileExt(fileExt) = False then
            msg="上传失败!只允许上传这几种文件类型:" & replace(upfileallType,"|",",")
            FoundErr=true
        end if
        
        if FoundErr<>true then

            filename=FormatName(fileExt)
            myfileName=filename
            filename=SavePath&filename

            ofile.SaveToFile Server.mappath(FileName)   '保存文件
            
            Session("filesize")=Session("filesize")+oFileSize
            response.write "文件上传成功!文件大小为:" &FormatFileSize(oFileSize)&""
            response.write" <a href='?action=add&formName="&myformName&"&SavePath="&mySavePath&"'><font color=red>继续上传</font></a>"
Response.Write "<script>parent.AddItem('" &myfileName& "');</script>"
        else
            response.write("<font color=red>" & msg & "</font>&nbsp;<b><a href='javascript:history.back()'><font color=blue>返回<font></a></b>")
        end if
        
        set ofile=nothing
    next
    set upload=nothing
end sub


Function CheckFileExt(FileExt)
        Dim Forumupload,i
        CheckFileExt=False
        If FileExt="" or IsEmpty(FileExt) Then
            CheckFileExt = False
            Exit Function
        End If
        If FileExt="asp" or FileExt="asa" or FileExt="aspx" or FileExt="cer" or FileExt="cdx" then
            CheckFileExt = False
            Exit Function
        End If
        Forumupload = Split(LCase(upfileallType),"|")
        For i = 0 To ubound(Forumupload)
            If FileExt = Trim(Forumupload(i)) Then
                CheckFileExt = True
                Exit Function
            Else
                CheckFileExt = False
            End If
        Next
End Function
Function FormatName(Byval FileExt)
        Dim RanNum,TempStr
        Randomize
        RanNum = Int(90000*rnd)+10000
        TempStr = Year(now) & Month(now) & Day(now) & Hour(now) & Minute(now) & Second(now) & RanNum & "." & FileExt
        FormatName = TempStr
End Function

function FormatFileSize(xxx)
dim show11
show11=xxx & " Byte"
if xxx>=1024 then
   xxx=(xxx/1024)
   show11=round(xxx,2) & " KB"
end if
if xxx>=1024 then
   xxx=(xxx/1024)
   show11=round(xxx,2) & " MB"
end if
FormatFileSize=cstr(show11)
end function

Function CreatePath(PathValue)
    Dim objFSO,Fsofolder,uploadpath
        uploadpath = Year(Date) & "-" & Month(Date)
    If Right(PathValue,1)<>"/" Then PathValue = PathValue&"/"
    On Error Resume Next
    Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
    If objFSO.FolderExists(Server.MapPath(PathValue & uploadpath))=False Then
        objFSO.CreateFolder Server.MapPath(PathValue & uploadpath)
    End If
    If Err.Number = 0 Then
        CreatePath = PathValue & uploadpath & "/"
    Else
        CreatePath = PathValue
    End If
    Set objFSO = Nothing
End Function
%>

[ 本帖最后由 ljjlxj 于 2013-3-28 08:49 编辑 ]
2013-03-27 20:45
elongtown
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:12
帖 子:138
专家分:1003
注 册:2013-2-18
收藏
得分:5 
这是一般要求,要使用原来的名字,比如中文名、文件重名的不能保证不出错
2013-03-28 08:55
ljjlxj
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-3-27
收藏
得分:0 
那要怎么改才能实现使用原来的名字呢
2013-03-28 09:03
ljjlxj
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-3-27
收藏
得分:0 
回复 5楼 ljjlxj
那位高手帮一下!!
2013-03-28 16:46
hu9jj
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:红土地
等 级:贵宾
威 望:400
帖 子:11857
专家分:43421
注 册:2006-5-13
收藏
得分:5 
用原来的名字就会有重名的可能性,所以就会用日期时间或其他规律来产生一个唯一的文件名。

活到老,学到老!http://www.(该域名已经被ISP盗卖了)E-mail:hu-jj@
2013-03-29 07:59
ljjlxj
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2013-3-27
收藏
得分:0 
有重名不要紧,在数据库里有唯一的ID啊。
2013-03-29 15:25
hu9jj
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:红土地
等 级:贵宾
威 望:400
帖 子:11857
专家分:43421
注 册:2006-5-13
收藏
得分:0 
以下是引用ljjlxj在2013-3-29 15:25:25的发言:

有重名不要紧,在数据库里有唯一的ID啊。
问题是操作系统的文件管理不允许出现重名文件。

活到老,学到老!http://www.(该域名已经被ISP盗卖了)E-mail:hu-jj@
2013-03-29 17:01
sylknb
Rank: 4
等 级:贵宾
威 望:14
帖 子:1547
专家分:184
注 册:2006-6-3
收藏
得分:5 
<!--#include file="inc/upfile_class.asp" -->
<% If Session("Admin")="" Then Response.Redirect "login.asp" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>文件上传</title>
<Script language="javascript">
function CheckUp()
{
    if(document.upfileFrm.UpFile.value == "")
    {
        alert('请先选择上传文件!');
        document.upfileFrm.UpFile.focus();
        return false;
    }
}
</Script>
<link href="inc/main.css" rel="stylesheet" type="text/css">
</head
><body>
<%    Dim upfolder,upformname,backname
    upfolder = Request("upfolder")   
    upformname = Request("upformname")
    backname = Request("backname")   

    If Request("action") = "UploadFile" Then
        Call Save_Up()
    End If
   
    Sub Save_Up()
        Dim upfolder,upformname,backname,upFile
        Dim oFile,FSPath,formName,newFileName
        upfolder = "..\"&Request("upfolder")
        upformname = Request("upformname")
        backname = Request("backname")     
        Set upFile = New upfile_class         
        upFile.AllowExt = "jpg;jpeg;gif;bmp"
        upFile.GetData (10240000)
        If upFile.isErr = 0 Then
            FSPath = Server.MapPath(upfolder)&"\"
            
            
            For Each formName In upFile.file
                Set oFile = upFile.file(formName)
                newFileName = upFile.AutoSave(formName,FSPath&oFile.filename)如2011070711452752825.jpg
                改为:newFileName = upFile.Savefile(formName,FSPath&oFile.filename)如:51011001

                response.write "newFileName="&newFileName
               
                If upfile.isErr Then            
                    Response.Write upfile.errmessage &"<a href=upload.asp>返回</a>"
                    Response.End
                Else                 
                    Response.Write "<Script>parent."&upformname&"." &backname& ".value = '"&newFileName&"'</Script>"2011070711452752825.jpg

                 改为:value = '"&newFileName&"'-->'"&newFileName"'&".扩展名"&"'
                      显50111001.jpg

   Response.Write "文件上传成功!"
                    Response.End
                End If
                Set oFile=nothing            
            Next
        Else
            Response.Write upfile.errmessage &"<a href=upload.asp>[返回]</a>"
            Response.End
        End If
        Set upFile=nothing
    End Sub
%>
<form action="?action=UploadFile&upfolder=<%=upfolder%>&upformname=<%=upformname%>&backname=<%=backname%>" name="upfileFrm" method="POST" enctype="multipart/form-data" onSubmit="return(CheckUp());">
  <input name="UpFile" type="file" size="40">
  <input type="submit" name="Submit" value="上 传">
</form>
</body>
</html>

2013-04-03 11:57
快速回复:上传附件
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017889 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved