Duplicate file name bug

Last post 12-29-2011, 3:52 AM by snaack. 3 replies.
Sort Posts: Previous Next
  •  12-28-2011, 6:55 AM 72444

    Duplicate file name bug

    Hi there,
     
    I have a question similar to http://cutesoft.net/forums/thread/71622.aspx
     
    The following:
     
    I need to store the location of an uploaded file in a database. I access the FileName property of the uploaded file as such:
     
    1. Dim mvcfile      
    2. Set mvcfile=uploader.GetUploadedFile(Request.Form("uploader"))     
    3. Response.Write mvcfile.FileName  
     The problem with this is however that this property contains the original name of the file, and not the name of the file after is has been persisted to the location as set in the SaveDirectory property. No problem in case of unique uploads.
     
    However, in case of a duplicate upload the file in the SaveDirectory location gets renamed by the component. A timestamp is appended to the file name. The FileName property still contains the original name though.
     
    Obviously I need the name of the persisted file. How can I access this?
     
    Thanks and regards,
    Nico 
     
     
  •  12-28-2011, 1:06 PM 72449 in reply to 72444

    Re: Duplicate file name bug

    Hi snaack,
     
    For now can not get the new name of the duplicate upload the file.
     
    I suggest you use the "CopyTo" method to save the upload file. In this method you can handle the file name and the file save location.
     
    About the same file name problem, please refer to http://cutesoft.net/forums/thread/59459.aspx.
     
    Regards,
     
    Ken 
  •  12-29-2011, 1:27 AM 72451 in reply to 72449

    Re: Duplicate file name bug

    Alright, thanks for your swift reply Ken. I will script a workaround for this. 
     
    To further clarify myself, consider this situation:
     
    File "abc.jpg" gets uploaded from computer A. It is persisted as "abc.jpg" and the value of the FileName-property is "abc"jpg".
    File "abc.jpg" gets uploaded from computer B. Since a file named "abc.jpg" already exists on the server, this duplicate file is persisted as "abc_201129120824.jpg" (or something like that). The value of the FileName-property, however, is "abc.jpg" and points to the first file, not to this one.
     
    To me, this behavior seems unintended. It is unpractical at minimum to be forced to script a workaround for this.
     
    Do you consider this to be a bug, and will this be fixed in a future release?? 
     
    Regards,
    Nico 
  •  12-29-2011, 3:52 AM 72455 in reply to 72451

    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 as RSS news feed in XML