Re: Duplicate file name bug

  •  12-29-2011, 3:52 AM

    Re: Duplicate file name bug

    Hi Ken,
     
     For the sake of other users facing the same problem, here is my (incomplete) solution:
     
    - Get the FilePath property. This contains the full path of the uploaded file in the temporary folder
    - Get the FileName property. This contains the original filename 
    - Check it a file with the name as the value of the FileName property already exists in the SaveFolder
    - If so - copy the file from the temporary directory to the SaveFolder with  a new unique name
    - Else, do nothing, you can use the FileName property / file gets properly uploaded
     
    Note: the ASPUpload component also persists the uploaded file in the SaveFolder using a unique filename in case of a duplicate.  If you wish to delete this (obsolete) file, perhaps you could do some magic with the timestamp of files in the SaveFile location. I didn't.
     
    My script looks something like this: 
    1. Function UniqueFileAppendString()  
    2.     UniqueFileAppendString = "_" &_   
    3.         CStr(Year(Now)) & CStr(Month(Now)) & CStr(Day(Now)) & "_" &_  
    4.         CStr(Minute(Now)) & "_" &_  
    5.          CStr(Second(Now))  
    6. End Function  
    7.   
    8. If Request.Form("uploader")&""<>"" Then     
    9.     Dim mvcfile, newFileName, filePath   
    10.     Set mvcfile=uploader.GetUploadedFile(Request.Form("uploader"))     
    11.     newFileName = mvcfile.FileName  
    12.     filePath = mvcfile.FilePath  
    13.   
    14.     If FileExists(newFileName) = True Then  
    15.         ' append a timestamp to the name of the file  
    16.         newFileName = Left(mvcfile.FileName, InStrRev(mvcfile.FileName, ".") -1) & _  
    17.                       UniqueFileAppendString() & _  
    18.                       Right(mvcfile.FileName, InStrRev(mvcfile.FileName, ".") +1 )  
    19.   
    20.         ' now copy the file from the Temp location to the desired location  
    21.         Dim fso, file  
    22.         Set fso = Server.CreateObject("Scripting.FileSystemObject")  
    23.         Set file = fso.GetFile(filePath) ' File pointer to the temp file  
    24.   
    25.         file.Copy Server.MapPath(uploadURL & newFileName), true ' the actual copy  
    26.         Set fso = Nothing ' cleanup  
    27.         Set file = Nothing ' cleanup  
    28.     End If  
    29.   
    30.     Set mvcfile = Nothing ' cleanup  
    31.   
    32. End If  
    Regards,
    Nico 
View Complete Thread