| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 389 人关注过本帖
标题:[求助]怎样让上传文件自动命名,并将新名与原名同时写入数据库
取消只看楼主 加入收藏
thbwn
Rank: 1
等 级:新手上路
帖 子:235
专家分:0
注 册:2007-10-2
结帖率:80%
收藏
 问题点数:0 回复次数:0 
[求助]怎样让上传文件自动命名,并将新名与原名同时写入数据库

这段代码上传时,文件名以原名上传,文件名相同时是以在后面加-1、-2……已示区分。能否实现这种效果:
1、上传后,随机命名,年月日时间+随机码+扩展名
2、原文件名写入数据库,在show.asp将新名与原名一并显示出来,因为新名便于保存,但不便于知道是什么东西,有原名可大致知道此文件的内容。

<!--#include file="conn.asp"-->
<%
Response.Buffer = True
Server.ScriptTimeOut=9999999
On Error Resume Next
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta http-equiv="Content-Language" content="zh-cn" />
<meta content="all" name="robots" />
<meta name="author" content="木目,Woodeye" />
<meta name="description" content="木目ASP文件上传工具" />
<meta name="keywords" content="木目,ASP,Upload,文件上传" />
<style type="text/css">
<!--
body,input {font-size:12px;}
-->
</style>
<title>木目ASP文件上传工具(川江号子修改版)</title>
</head>
<body id="body">
<%
ExtName = "jpg,gif,bmp,chm,exe,png,txt,rar,zip,doc,htm,html,asp" '允许扩展名
If Right(SavePath,1)<>"/" Then SavePath=SavePath&"/" '在目录后加(/)
CheckAndCreateFolder(SavePath)

UpLoadAll_a = Request.TotalBytes '取得客户端全部内容
If(UpLoadAll_a>0) Then
Set UploadStream_c = Server.CreateObject("ADODB.Stream")
UploadStream_c.Type = 1
UploadStream_c.Open
UploadStream_c.Write Request.BinaryRead(UpLoadAll_a)
UploadStream_c.Position = 0

FormDataAll_d = UploadStream_c.Read
CrLf_e = chrB(13)&chrB(10)
FormStart_f = InStrB(FormDataAll_d,CrLf_e)
FormEnd_g = InStrB(FormStart_f+1,FormDataAll_d,CrLf_e)

Set FormStream_h = Server.Createobject("ADODB.Stream")
FormStream_h.Type = 1
FormStream_h.Open
UploadStream_c.Position = FormStart_f + 1
UploadStream_c.CopyTo FormStream_h,FormEnd_g-FormStart_f-3
FormStream_h.Position = 0
FormStream_h.Type = 2
FormStream_h.CharSet = "GB2312"
FormStreamText_i = FormStream_h.Readtext
FormStream_h.Close

FileName_j = Mid(FormStreamText_i,InstrRev(FormStreamText_i,"\")+1,FormEnd_g)

If(CheckFileExt(FileName_j,ExtName)) Then
SaveFile = Server.MapPath(SavePath & FileName_j)

If Err Then
Response.Write "文件上传: <span style=""color:red;"">文件上传出错!</span> <a href=""" & Request.ServerVariables("URL") &""">重新上传文件</a><br />"
Err.Clear
Else
SaveFile = CheckFileExists(SaveFile)

k=Instrb(FormDataAll_d,CrLf_e&CrLf_e)+4
l=Instrb(k+1,FormDataAll_d,leftB(FormDataAll_d,FormStart_f-1))-k-2
FormStream_h.Type=1
FormStream_h.Open
UploadStream_c.Position=k-1
UploadStream_c.CopyTo FormStream_h,l
FormStream_h.SaveToFile SaveFile,2

SaveFileName = Mid(SaveFile,InstrRev(SaveFile,"\")+1)

uptime=now

'//写入到数据库
set rs=server.CreateObject("adodb.recordset")
sql="select*from file"
rs.open sql,conn,1,2
rs.addnew
rs("fileName")=SaveFileName
rs("uptime")=uptime
rs("contentlen")=UpLoadAll_a/1024
rs.update
set rs=nothing
conn.close
set conn=nothing

Response.write "<center><br><br><br><br><br><br><br><br><br><br>文件上传成功,文件路径:"& SavePath &"" & SaveFileName & "<br><br><a href=""" & Request.ServerVariables("URL") &""">继续上传文件</a> &nbsp;&nbsp;&nbsp; <a href='Show.asp'>管理上传文件</a></center>"
End If
Else
Response.write "<script>alert('文件格式不正确,上传失败!');location.replace('upload.asp')</script>"
End If

Else
%>
<script language="Javascript">
<!--
function ValidInput()
{

if(document.upform.upfile.value=="")
{
alert("请选择上传文件!")
document.upform.upfile.focus()
return false
}
return true
}
// -->
</script>
<table cellpadding="0" cellspacing="0" width="100%" border="0"><tr><td height="130"></td></tr></table>
<table border="1" cellspacing="1" cellpadding="5" align="center" height="10" bordercolor="#7C96B8" bordercolordark="#FFFFFF">
<form action='<%= Request.ServerVariables("URL") %>' method='post' name="upform" onsubmit="return ValidInput()" enctype="multipart/form-data">
<TR bgcolor="#7C96B8">
<td height="20" colspan="2"><div align="center"><font color="#FFFFFF">文件上传</font></div></td>
</TR>
<tr>
<td width="60">选择文件:</td>
<td><input name="upfile" type="file"></td>
</tr>
<tr align="center">
<td height="16" colspan="2"><input type="submit" name="Submit" value="上传">
<input type="reset" name="Submit2" value="重置"></td>
</tr>
</form>
</table>
<%
End if
Set FormStream_h = Nothing
UploadStream.Close
Set UploadStream = Nothing
%>
</body>
</html>
<%
'判断文件类型是否合格
Function CheckFileExt(FileName,ExtName) '文件名,允许上传文件类型
FileType = ExtName
FileType = Split(FileType,",")
For i = 0 To Ubound(FileType)
If LCase(Right(FileName,3)) = LCase(FileType(i)) then
CheckFileExt = True
Exit Function
Else
CheckFileExt = False
End if
Next
End Function

'检查上传文件夹是否存在,不存在则创建文件夹
Function CheckAndCreateFolder(FolderName)
dim fldr
fldr = Server.Mappath(FolderName)
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(fldr) Then
fso.CreateFolder(fldr)
End If
Set fso = Nothing
End Function

'检查文件是否存在,重命名存在文件
Function CheckFileExists(FileName)
Set fso=Server.CreateObject("Scripting.FileSystemObject")
If fso.FileExists(SaveFile) Then
i=1
msg=True
Do While msg
CheckFileExists = Replace(SaveFile,Right(SaveFile,4),"_" & i & Right(SaveFile,4))
If not fso.FileExists(CheckFileExists) Then
msg=False
End If
i=i+1
Loop
Else
CheckFileExists = FileName
End If
Set fso=Nothing
End Function
%>

[此贴子已经被作者于2007-10-5 15:50:22编辑过]

搜索更多相关主题的帖子: 数据库 原名 文件 自动 
2007-10-05 15:46
快速回复:[求助]怎样让上传文件自动命名,并将新名与原名同时写入数据库
数据加载中...
 
   



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

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