2015-08-31 159 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:不要使用用户提供的文件名,新建一个。这将避免服务器黑客入侵。