在ASP文件中,要實(shí)現(xiàn)上傳后自動(dòng)重命名,你可以使用以下方法:
<%
Dim newFileName, fileExtension, uuid
newFileName = ""
fileExtension = ".jpg" ' 你可以根據(jù)需要修改文件擴(kuò)展名
uuid = CreateObject("Scriptlet.Util").NewGuid()
newFileName = uuid & fileExtension
%>
Dim targetFolderPath
targetFolderPath = "C:\uploads\" ' 你可以將此路徑更改為你的目標(biāo)文件夾路徑
fullPath = targetFolderPath & newFileName
On Error Resume Next
Set objFolder = CreateObject("Scripting.FileSystemObject")
objFolder.CreateDirectory targetFolderPath, True
On Error Goto 0
Dim fileInput, fileBytes, file
Set fileInput = Request.Form("fileInput") ' 假設(shè)你的文件輸入字段的名稱為"fileInput"
fileBytes = fileInput.BinaryContent
Set file = Server.CreateObject("ADODB.Stream")
file.Open
file.Write fileBytes
file.SaveToFile fullPath, 2 ' 2表示覆蓋現(xiàn)有文件
這樣,當(dāng)用戶上傳文件時(shí),ASP腳本將自動(dòng)生成一個(gè)新的唯一文件名,并將文件保存到指定的目標(biāo)文件夾中。