上传文件时怎么把文件名字改了???

jiangdiqiuyun 2005-05-28 04:34:07
我在原有的文件上改
各位帮忙看看
以下是upfile.asp
<%type9=request("type9")
zuozhe=request("guo")
'response.Write type9
'response.Write zuozhe
'response.End
%>
<!--#include file="uploadx.asp"-->
<!--#include file='connect.asp'-->
<html>
<head>
<title>上传文件中.....</title>
</head>
<body>
<%
'on error resume next
Dim filePath
Dim fileName
Dim fileExt
Dim file_subject
Dim Sql
Dim msg
Dim errflag
Dim errnumber
Dim SavePath
Dim maxfilesize

SavePath = "SavePath" '虚拟路径(后面不要加"/"符号)
maxfilesize = 1*1024 '大小为1M

Errflag=false
filePath = SavePath '使用虚拟路径进行赋值,如"/www"或"www"等
filePath = Server.MapPath(filePath) '将虚拟路径转换为磁盘路径
file_subject = GetFormVal("filename") '取得文件标题
fileext = GetFormVal("fileExt") '取得文件介绍
errnumber = GetFormVal("errnumber") '取得报错方式
errnumber = cint(errnumber)

if len(trim(file_subject))=0 then
Response.Write "文件主题不能为空"
Response.End
end if
if len(trim(fileext))=0 then
fileext = "无简介"
end if

filename = SaveFile("fruit",filePath,maxfilesize,errnumber,1) '保存并取得文件名
' 0,1 唯一文件名方式,如果有同名则自动改名;
' 1,1 报错方式,如果有同名则出错;
' 2,[0|1] 覆盖方式,如果有同名则覆盖原来的文件

if len(trim(filename))>0 then
Dim PerFnN
Dim intPerFnN
Dim PerFsize

PerFnN=split(filename,"|")
intPerFnN=Ubound(PerFnN)
Select Case intPerFnN
Case 1
FileName=Trim(PerFnN(0))
PerFsize=Csng(Trim(PerFnN(1)))
Case 0
FileName=Trim(PerFnN(0))
PerFsize=0
End Select

select case Trim(filename)
case "pathError"
msg="错误: 指定的路径不存在"
errflag=true
case "refileError"
msg="错误: 文件已经存在"
errflag=true
case "sizeError"
msg="错误: 文件超出指定大小"
errflag=true
case "modeError"
msg="主机在不支持Fso模式下不能采用唯一或报错方式上传文件"
errflag=true
case "fileError"
msg="被限制上传的文件格式"
errflag=true
case else
msg=""
errflag=false
end select
shijian=now()
FileName_2=year(shijian)&month(shijian)&day(shijian)&hour(shijian)&minute(shijian)&second(shijian)
if not errflag then
Sql = "insert into upfile_table (subject,expit,filepath,filename,filesize,type9,zuozhe,FileName_2) values"
Sql = Sql& " ('"& file_subject &"','"& fileext &"','"& SavePath &"','"& filename &"',"& PerFsize &",'"&type9 &"','"&zuozhe &"','"&FileName_2 &"')"
conn.execute(sql)
end if
end if
conn.close
set conn=nothing

Response.Write "<script language='Jscript'>"&vbcrlf
Response.Write "<!--"&vbcrlf
if errflag then
Response.Write "alert('"& msg &"');"&vbcrlf
end if
Response.Write "window.open('default.asp?type9="&type9&"','_self');"
Response.Write "//-->"&vbcrlf
Response.Write "</script>"&vbcrlf

%>
</body>
</html>
...全文
423 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
binbin695 2005-05-28
  • 打赏
  • 举报
回复
你用的是稻香老农吧,上舒传后时按时间和三个随机数
jiangdiqiuyun 2005-05-28
  • 打赏
  • 举报
回复
其实就是


filename = SaveFile("fruit",filePath,maxfilesize,errnumber,1)

保存并取得文件名

怎么让他在文件上传前把文件名改成别的呢?
创造奇迹9999 2005-05-28
  • 打赏
  • 举报
回复
O!
一进来的看到那么多的代码!
asd119cn 2005-05-28
  • 打赏
  • 举报
回复
http://rumor.17560.net/

风声的无组件上传类,用法挺简单的。建议你去看看。还有一些别的小工具,都挺好用的。
ddd880 2005-05-28
  • 打赏
  • 举报
回复
你把我发的2个程序复制用下 就知道效果了
ddd880 2005-05-28
  • 打赏
  • 举报
回复
<SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT>
dim Data_5xsoft

Class upload_5xsoft

dim objForm,objFile,Version

Public function Form(strForm)
strForm=lcase(strForm)
if not objForm.exists(strForm) then
Form=""
else
Form=objForm(strForm)
end if
end function

Public function File(strFile)
strFile=lcase(strFile)
if not objFile.exists(strFile) then
set File=new FileInfo
else
set File=objFile(strFile)
end if
end function


Private Sub Class_Initialize
dim RequestData,sStart,vbCrlf,sInfo,iInfoStart,iInfoEnd,tStream,iStart,theFile
dim iFileSize,sFilePath,sFileType,sFormValue,sFileName
dim iFindStart,iFindEnd
dim iFormStart,iFormEnd,sFormName
Version="化境HTTP上传程序 Version 2.0"
set objForm=Server.CreateObject("Scripting.Dictionary")
set objFile=Server.CreateObject("Scripting.Dictionary")
if Request.TotalBytes<1 then Exit Sub
set tStream = Server.CreateObject("adodb.stream")
set Data_5xsoft = Server.CreateObject("adodb.stream")
Data_5xsoft.Type = 1
Data_5xsoft.Mode =3
Data_5xsoft.Open
Data_5xsoft.Write Request.BinaryRead(Request.TotalBytes)
Data_5xsoft.Position=0
RequestData =Data_5xsoft.Read

iFormStart = 1
iFormEnd = LenB(RequestData)
vbCrlf = chrB(13) & chrB(10)
sStart = MidB(RequestData,1, InStrB(iFormStart,RequestData,vbCrlf)-1)
iStart = LenB (sStart)
iFormStart=iFormStart+iStart+1
while (iFormStart + 10) < iFormEnd
iInfoEnd = InStrB(iFormStart,RequestData,vbCrlf & vbCrlf)+3
tStream.Type = 1
tStream.Mode =3
tStream.Open
Data_5xsoft.Position = iFormStart
Data_5xsoft.CopyTo tStream,iInfoEnd-iFormStart
tStream.Position = 0
tStream.Type = 2
tStream.Charset ="gb2312"
sInfo = tStream.ReadText
tStream.Close
'取得表单项目名称
iFormStart = InStrB(iInfoEnd,RequestData,sStart)
iFindStart = InStr(22,sInfo,"name=""",1)+6
iFindEnd = InStr(iFindStart,sInfo,"""",1)
sFormName = lcase(Mid (sinfo,iFindStart,iFindEnd-iFindStart))
'如果是文件
if InStr (45,sInfo,"filename=""",1) > 0 then
set theFile=new FileInfo
'取得文件名
iFindStart = InStr(iFindEnd,sInfo,"filename=""",1)+10
iFindEnd = InStr(iFindStart,sInfo,"""",1)
sFileName = Mid (sinfo,iFindStart,iFindEnd-iFindStart)
theFile.FileName=getFileName(sFileName)
theFile.FilePath=getFilePath(sFileName)
'取得文件类型
iFindStart = InStr(iFindEnd,sInfo,"Content-Type: ",1)+14
iFindEnd = InStr(iFindStart,sInfo,vbCr)
theFile.FileType =Mid (sinfo,iFindStart,iFindEnd-iFindStart)
theFile.FileStart =iInfoEnd
theFile.FileSize = iFormStart -iInfoEnd -3
theFile.FormName=sFormName
if not objFile.Exists(sFormName) then
objFile.add sFormName,theFile
end if
else
'如果是表单项目
tStream.Type =1
tStream.Mode =3
tStream.Open
Data_5xsoft.Position = iInfoEnd
Data_5xsoft.CopyTo tStream,iFormStart-iInfoEnd-3
tStream.Position = 0
tStream.Type = 2
tStream.Charset ="gb2312"
sFormValue = tStream.ReadText
tStream.Close
if objForm.Exists(sFormName) then
objForm(sFormName)=objForm(sFormName)&", "&sFormValue
else
objForm.Add sFormName,sFormValue
end if
end if
iFormStart=iFormStart+iStart+1
wend
RequestData=""
set tStream =nothing
End Sub

Private Sub Class_Terminate
if Request.TotalBytes>0 then
objForm.RemoveAll
objFile.RemoveAll
set objForm=nothing
set objFile=nothing
Data_5xsoft.Close
set Data_5xsoft =nothing
end if
End Sub


Private function GetFilePath(FullPath)
If FullPath <> "" Then
GetFilePath = left(FullPath,InStrRev(FullPath, "\"))
Else
GetFilePath = ""
End If
End function

Private function GetFileName(FullPath)
If FullPath <> "" Then
GetFileName = mid(FullPath,InStrRev(FullPath, "\")+1)
Else
GetFileName = ""
End If
End function
End Class

Class FileInfo
dim FormName,FileName,FilePath,FileSize,FileType,FileStart
Private Sub Class_Initialize
FileName = ""
FilePath = ""
FileSize = 0
FileStart= 0
FormName = ""
FileType = ""
End Sub

Public function SaveAs(FullPath)
dim dr,ErrorChar,i
SaveAs=true
if trim(fullpath)="" or FileStart=0 or FileName="" or right(fullpath,1)="/" then exit function
set dr=CreateObject("Adodb.Stream")
dr.Mode=3
dr.Type=1
dr.Open
Data_5xsoft.position=FileStart
Data_5xsoft.copyto dr,FileSize
dr.SaveToFile FullPath,2
dr.Close
set dr=nothing
SaveAs=false
end function
End Class
</SCRIPT>
ddd880 2005-05-28
  • 打赏
  • 举报
回复
<!--#include file="upload_5xsoft.inc"-->
<body bgcolor="#6A71A3" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<%
if Request.Querystring("Post")="OK" then
set upload=new upload_5xsoft
set file=upload.file("pic")

if file.fileSize>0 then
fname=file.FileName

Ext = split(file.fileName,".")
FileExt = Ext(ubound(Ext))'取得文件名后缀

'if inStr(fname,"jpg")=0 and inStr(fname,"gif")=0 and inStr(fname,"png")=0 then

if fileExt<>"jpg" OR fileExt<>"gif" OR fileExt<>"png" then
response.Write("您输入了错误的图片信息,请<a href=# onCLICK='history.back(-1)'>返回</a>")
response.end
end if
end if

fname="../tupianpic/"&fname
file.saveAs Server.mappath(fname)
end if

j=len(fname)
fname=Mid(fname,4,j)

session("uptupian")=fname
response.Write "<div style='color:white;font-size:9pt;'>图片: <b>"&fname&"</b> 已经成功上传!</div>"
response.end

set file=nothing
set upload=nothing

end if
%>
<form action="<%=filename%>?Post=OK" method="post" enctype="multipart/form-data" name="up2">
<input name="pic" type="file" id="pic">
<input type="submit" name="Submit3" value="上传">
</form>
jiangdiqiuyun 2005-05-28
  • 打赏
  • 举报
回复
以下是uploadx.asp
<%
Dim FormData, FormSize, Divider, bCrLf
Dim FixFileExt

FormSize = Request.TotalBytes
FormData = Request.BinaryRead(FormSize)
bCrLf = ChrB(13) & ChrB(10)
Divider = LeftB(FormData, InStrB(FormData, bCrLf) - 1)
FixFileExt="asp|aspx|asa|asax|ascx|ashx|asmx|axd|cdx|cer|config|cs|csproj|licx|rem|resx|shtml|shtm|soap|stm|vb|vbproj|webinfo|cgi|pl|php|phtml|php3" '限制为只有这些文件可以上传(用"|"号格开)

Function SaveFile(FormFileField, Path, MaxSize, SavType, FsoType)
If (SavType=0 or SavType=1) and FsoType=0 then
SaveFile = "modeError"
Exit function
End if

Dim ObjStream,Allyes_ObjStream
Dim StartPos
Dim Strlen, SearchStr
Dim FileStart, FileLen, FileContent
Dim Re_SavType
Dim fnN
Dim intfnN
Dim FileExtName
Dim FixFnN
Dim intFix
Dim i

Set ObjStream = Server.CreateObject("ADODB.Stream")
Set Allyes_ObjStream = Server.CreateObject("ADODB.Stream")
ObjStream.Mode = 3
ObjStream.Type = 1
Allyes_ObjStream.Mode = 3
Allyes_ObjStream.Type = 1
SaveFile = ""
StartPos = LenB(Divider) + 2
FormFileField = Chr(34) & FormFileField & Chr(34)

'-----------------------------------检测路径------------------------------------
If Right(Path,1) <> "\" Then '检测目录参数的完整性
Path = Path & "\"
End If
If FsoType = 1 then '如果支持FSO则检测。否则不检测
CheckPath(path) '检测指定目录是否存在,如果不存在,则自行创建
End if
'-------------------------------------------------------------------------------
If len(trim(MaxSize)) = 0 then
MaxSize=50*1024 '指定默认最大上传文件为50M
End if

Do While StartPos > 0 '开始保存每个file文件对象数据
strlen = InStrB(StartPos, FormData, bCrLf) - StartPos
SearchStr = MidB(FormData, StartPos, strlen)
If InStr(bin2str(SearchStr), FormFileField) > 0 Then
FileName = bin2str(GetFileName(SearchStr,path,SavType,FsoType))

''----------------文件格式限制------------------------
fnN = split(fileName,".")
intfnN = Ubound(fnN)
FileExtName = trim(fnN(intfnN))
FixFnN = Split(FixFileExt,"|")
intFix = Ubound(FixFnN)
for i = 0 to intFix
if lcase(FileExtName) = lcase(trim(FixFnN(i))) then
SaveFile = "fileError"
exit do
end if
next
'------------------------------------------------------

If FileName <> "" Then
FileStart = InStrB(StartPos, FormData, bCrLf & bCrLf) + 4
FileLen = InStrB(StartPos, FormData, Divider) - 2 - FileStart
If FileLen <= MaxSize*1024 Then
FileContent = MidB(FormData, FileStart, FileLen)
Allyes_ObjStream.Open
With ObjStream
.Open
.Write FormData
.Position=FileStart-1
.CopyTo Allyes_ObjStream,FileLen
End With

Re_SavType = SavType
If SavType = 0 Then
SavType = 1
End If

On error resume next
Allyes_ObjStream.SaveToFile Path & FileName, SavType
if err.number<>0 then
If Re_SavType=0 or Re_SavType=2 then
FileName="pathError"
else
FileName="refileError"
end if
end if
ObjStream.Close
Allyes_ObjStream.Close

If SaveFile <> "" Then
SaveFile = SaveFile & "," & FileName &"|"& FileLen
Else
SaveFile = FileName &"|"& FileLen
End If
Else
If SaveFile <> "" Then
SaveFile = SaveFile & ",refileError"
Else
SaveFile = "sizeError"
End If
End If
End If
End If
If InStrB(StartPos, FormData, Divider) < 1 Then
Exit Do
End If
StartPos = InStrB(StartPos, FormData, Divider) + LenB(Divider) + 2
Loop
End Function

Function GetFormVal(FormName) '取得如果是表单项目的过程
Dim StartPos
Dim Strlen, SearchStr
Dim ValStart, ValLen, ValContent

GetFormVal = ""
StartPos = LenB(Divider) + 2
FormName = Chr(34) & FormName & Chr(34)
Do While StartPos > 0
Strlen = InStrB(StartPos, FormData, bCrLf) - StartPos
SearchStr = MidB(FormData, StartPos, strlen)
If InStr(bin2str(SearchStr), FormName) > 0 Then
ValStart = InStrB(StartPos, FormData, bCrLf & bCrLf) + 4
ValLen = InStrB(StartPos, FormData, Divider) - 2 - ValStart
ValContent = MidB(FormData, ValStart, ValLen)
If GetFormVal <> "" Then
GetFormVal = GetFormVal & "," & bin2str(ValContent)
Else
GetFormVal = bin2str(ValContent)
End If
End If
If InStrB(StartPos, FormData, Divider) < 1 Then
Exit Do
End If
StartPos = InStrB(StartPos, FormData, Divider) + LenB(Divider) + 2
Loop
End Function

Function bin2str(binstr)
Dim BytesStream,StringReturn

Set BytesStream = Server.CreateObject("ADODB.Stream")
With BytesStream
.Type = 2
.Open
.WriteText binstr
.Position = 0
.Charset = "GB2312"
.Position = 2
StringReturn = .ReadText
.close
End With
Set BytesStream = Nothing
bin2str = StringReturn
End Function


Function str2bin(str)
Dim i
For i = 1 To Len(str)
str2bin = str2bin & ChrB(Asc(Mid(str, i, 1)))
Next
End Function

Function GetFileName(str,path,savtype,fsotype)
Dim fs
Dim i
Dim hFileName
Dim rFileName

str = RightB(str,LenB(str)-InstrB(str,str2bin("filename="))-9)
GetFileName = ""
FileName = ""
For i = LenB(str) To 1 Step -1
If MidB(str, i, 1) = ChrB(Asc("\")) Then
FileName = MidB(str, i + 1, LenB(str) - i - 1)
Exit For
End If
Next

If fsotype=1 then '如果支持FSO,则执行FSO过程
Set fs = Server.CreateObject("Scripting.FileSystemObject")
If savtype = 0 and fs.FileExists(path & bin2str(FileName)) = True Then
hFileName = FileName
rFileName = ""
For i = LenB(FileName) To 1 Step -1
If MidB(FileName, i, 1) = ChrB(Asc(".")) Then
hFileName = LeftB(FileName, i-1)
rFileName = RightB(FileName, LenB(FileName)-i+1)
Exit For
End If
Next
For i = 0 to 9999
If fs.FileExists(path & bin2str(hFileName) & i & bin2str(rFileName)) = False Then
FileName = hFileName & str2bin(i) & rFileName
Exit For
End If
Next
End If
Set fs = Nothing
End If
GetFileName = FileName
End Function

Function CheckPath(path) '检测该目录是否存在,如果不存在,则建立该目录
Dim Fs
set Fs=server.CreateObject("scripting.filesystemobject")
if not fs.FolderExists(path) then
Fs.CreateFolder(path)
end if
set Fs = nothing
End function
%>

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧