ASP文件上傳后如何自動(dòng)重命名

asp
小樊
81
2024-10-15 04:38:46
欄目: 編程語言

在ASP文件中,要實(shí)現(xiàn)上傳后自動(dòng)重命名,你可以使用以下方法:

  1. 使用UUID(通用唯一標(biāo)識(shí)符)生成新的文件名。這樣可以確保文件名的唯一性,避免覆蓋現(xiàn)有文件。
<%
Dim newFileName, fileExtension, uuid
newFileName = ""
fileExtension = ".jpg" ' 你可以根據(jù)需要修改文件擴(kuò)展名
uuid = CreateObject("Scriptlet.Util").NewGuid()
newFileName = uuid & fileExtension
%>
  1. 將新文件名與目標(biāo)文件夾路徑結(jié)合,生成完整的文件路徑。
Dim targetFolderPath
targetFolderPath = "C:\uploads\" ' 你可以將此路徑更改為你的目標(biāo)文件夾路徑
fullPath = targetFolderPath & newFileName
  1. 檢查目標(biāo)文件夾是否存在,如果不存在則創(chuàng)建它。
On Error Resume Next
Set objFolder = CreateObject("Scripting.FileSystemObject")
objFolder.CreateDirectory targetFolderPath, True
On Error Goto 0
  1. 將上傳的文件保存到新的文件路徑。
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)有文件
  1. 如果需要,你還可以將新文件名添加到數(shù)據(jù)庫中,以便在需要時(shí)檢索或刪除文件。

這樣,當(dāng)用戶上傳文件時(shí),ASP腳本將自動(dòng)生成一個(gè)新的唯一文件名,并將文件保存到指定的目標(biāo)文件夾中。

0