2015-08-31 103 views
0

我使用asp classic。我想重新命名圖像文件,而我上傳圖像在我創建的網頁文件夾。請幫我解決這個問題。如何在上傳文件夾時重命名圖片文件名

如果在目標文件夾中的文件具有相同名稱(如lokesh.jpg)什麼,我上傳,較新的文件應該b自動重命名(如LOKESH(1).JPG)而不是覆蓋

我代碼是如下:

upload.asp

<% 
Class FileUploader 
Public Files 
Private mcolFormElem 

Private Sub Class_Initialize() 
Set Files = Server.CreateObject("Scripting.Dictionary") 
Set mcolFormElem = Server.CreateObject("Scripting.Dictionary") 
End Sub 

Private Sub Class_Terminate() 
If IsObject(Files) Then 
Files.RemoveAll() 
Set Files = Nothing 
End If 
If IsObject(mcolFormElem) Then 
mcolFormElem.RemoveAll() 
Set mcolFormElem = Nothing 
End If 
End Sub 

Public Property Get Form(sIndex) 
Form = "" 
If mcolFormElem.Exists(LCase(sIndex)) Then Form = mcolFormElem.Item(LCase(sIndex)) 
End Property 

Public Default Sub Upload() 
Dim biData, sInputName 
Dim nPosBegin, nPosEnd, nPos, vDataBounds, nDataBoundPos 
Dim nPosFile, nPosBound 

biData = Request.BinaryRead(Request.TotalBytes) 
nPosBegin = 1 
nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(13))) 

If (nPosEnd-nPosBegin) <= 0 Then Exit Sub 

vDataBounds = MidB(biData, nPosBegin, nPosEnd-nPosBegin) 
nDataBoundPos = InstrB(1, biData, vDataBounds) 

Do Until nDataBoundPos = InstrB(biData, vDataBounds & CByteString("--")) 

nPos = InstrB(nDataBoundPos, biData, CByteString("Content-Disposition")) 
nPos = InstrB(nPos, biData, CByteString("name=")) 
nPosBegin = nPos + 6 
nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(34))) 
sInputName = CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin)) 
nPosFile = InstrB(nDataBoundPos, biData, CByteString("filename=")) 
nPosBound = InstrB(nPosEnd, biData, vDataBounds) 

If nPosFile <> 0 And nPosFile < nPosBound Then 
Dim oUploadFile, sFileName 
Set oUploadFile = New UploadedFile 

nPosBegin = nPosFile + 10 
nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(34))) 
sFileName = CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin)) 
oUploadFile.FileName = Right(sFileName, Len(sFileName)-InStrRev(sFileName, "\")) 

Dim oFileExtension 
If sFileName <> "" then 
oFileExtension = (Right(sFileName, Len(sFileName)-InStrRev(sFileName, "."))) 
If oFileExtension <> "jpg" AND oFileExtension <> "jpeg" AND oFileExtension <> "gif" AND oFileExtension <> "pdf" then 
response.write("<h1>Post New File</h1><p><font color=#ff0000>An error has occurred while processing your request.<br><br>We are sorry, Extensions other than JPG, JPEG, Gif, PDF are not allowed to upload<p><b>Click <a href='javascript:history.go(-1);'>here</a> to go back and address the error.</b></font>") 
response.end 
Exit Sub 
End if 
end If 

nPos = InstrB(nPosEnd, biData, CByteString("Content-Type:")) 
nPosBegin = nPos + 14 
nPosEnd = InstrB(nPosBegin, biData, CByteString(Chr(13))) 
oUploadFile.ContentType = CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin)) 

nPosBegin = nPosEnd+4 
nPosEnd = InstrB(nPosBegin, biData, vDataBounds) - 2 
oUploadFile.FileData = MidB(biData, nPosBegin, nPosEnd-nPosBegin) 

If sfileName <> "" then 
If oUploadFile.FileSize > 10000000 Then 
response.write("<h1>Post New Image</h1><p><font color=#ff0000>An error has occurred while processing your request.<br><br>We are sorry,  Upload file containing 10000000(10mb) bytes only.<p><b>Click <a href='javascript:window:history.go(-1);'>here</a> to go back and address the  error.</b></font>") 
response.end 
Exit Sub 
End if 
End if 

If oUploadFile.FileSize > 0 Then Files.Add LCase(sInputName), oUploadFile 
Else 
nPos = InstrB(nPos, biData, CByteString(Chr(13))) 
nPosBegin = nPos + 4 
nPosEnd = InstrB(nPosBegin, biData, vDataBounds) - 2 
If Not mcolFormElem.Exists(LCase(sInputName)) Then mcolFormElem.Add LCase(sInputName), CWideString(MidB(biData, nPosBegin, nPosEnd-nPosBegin)) 
End If 

nDataBoundPos = InstrB(nDataBoundPos + LenB(vDataBounds), biData, vDataBounds) 
Loop 
End Sub 

'String to byte string conversion 
Private Function CByteString(sString) 
Dim nIndex 
For nIndex = 1 to Len(sString) 
CByteString = CByteString & ChrB(AscB(Mid(sString,nIndex,1))) 
Next 
End Function 

'Byte string to string conversion 
Private Function CWideString(bsString) 
Dim nIndex 
CWideString ="" 
For nIndex = 1 to LenB(bsString) 
CWideString = CWideString & Chr(AscB(MidB(bsString,nIndex,1))) 
Next 
End Function 
End Class 

Class UploadedFile 
Public ContentType 
Public FileName 
Public FileData 

Public Property Get FileSize() 
FileSize = LenB(FileData) 
End Property 

Public Sub SaveToDisk(sPath) 
Dim oFS, oFile 
Dim nIndex 

If sPath = "" Or FileName = "" Then Exit Sub 
If Mid(sPath, Len(sPath)) <> "\" Then sPath = sPath & "\" 

Set oFS = Server.CreateObject("Scripting.FileSystemObject") 
If Not oFS.FolderExists(sPath) Then Exit Sub 

Set oFile = oFS.CreateTextFile(sPath & FileName, True) 

For nIndex = 1 to LenB(FileData) 
oFile.Write Chr(AscB(MidB(FileData,nIndex,1))) 
Next 

oFile.Close 
End Sub 

Public Sub SaveToDatabase(ByRef oField) 
If LenB(FileData) = 0 Then Exit Sub 

If IsObject(oField) Then 
oField.AppendChunk FileData 
End If 
End Sub 

End Class 
%> 

submit.asp

<!-- #include file="upload.asp" --> 
<% 
response.buffer = true 

Dim Uploader, File, i, j 
Set Uploader = New FileUploader 

Uploader.Upload() 
Dim brandnm, filename 
brandnm = Uploader.form("brandname") 

Dim objRSa, objCmda, stra 
Set objCmda = server.CreateObject("adodb.connection") 
Set Objrsa = Server.CreateObject("ADODB.Recordset") 
objCmda.open MM_connDUdirectory_STRING 

stra = "SELECT * FROM brand" 
Objrsa.Open stra,objCmda,1,2 

if Uploader.Files.count <> 0 then 
File = Uploader.Files.Items() 
File(0).SavetoDisk Server.MapPath("upload/brands") 'Folder path where image will save 
filename = File(0).Filename 
else 
filename = "" 
End if 

Objrsa.addnew 
Objrsa.fields("brand_name") = brandnm 
Objrsa.fields("brand_createddt") = now() 
if filename <>"" then Objrsa.fields("brand_picpath") = filename 

For Each File In Uploader.Files.Items 
Objrsa("brand_ctype") = File.ContentType 
next 
Objrsa.Update 

Objrsa.Close 
Set Objrsa = Nothing 
set objCmda = Nothing 
%> 

請幫我解決這個問題。

+0

還在等待。幫我出去 –

+0

還在等.. –

回答

0

如果你想重新命名它遵循一個已知模式作爲你的榜樣(「文件名(編號).EXT」),您必須使用僞代碼:

let counter = 1 
let original = file(0).Filename 
let current = file(0).Filename 
while(current file exists) 
    current = original-without-extension + (counter) + original-extension 
    counter = counter + 1 
end 

然而,我認爲將用戶提供的文件名存儲到數據庫中會更好,並選擇一個隨機文件名將實際文件存儲到文件系統中。

let current = userLogin + (currentTime as yyyyMMddHHmmss) + ".uploaded" 

通過使用僞造的文件擴展名,你讓你的應用程序的方式更安全,因爲你的文件將不可解釋/可執行文件 - 想象一個惡意用戶上傳asp文件,並執行它。

如果這打破了圖像的MIME類型,您應該考慮創建另一個.ASP頁面來讀取數據庫,以根據用戶提供的文件擴展名來發現適當的MIME類型,編寫該內容類型和二進制文件內容。

TL; DR:不要使用用戶提供的文件名,新建一個。這將避免服務器黑客入侵。