OverwriteExistingFiles = True Inconsistency

7 posts, 0 answers
  1. Stephen
    Stephen avatar
    86 posts
    Member since:
    Jan 2011

    Posted 29 Mar 2012 Link to this post

    Hello,

    I am using the Q1 2012 release(2012.1.215.40)

    I think I found an inconsistency with the file handling when OverwriteExistingFiles = True.
    I am uploading 2 files to the same directory programmatically:
    Dim objBytes As Byte() = System.Text.Encoding.GetEncoding("UTF-8").GetBytes("This is file1")
            Using objStream As New System.IO.MemoryStream(objBytes)
                Dim uploadFile As New RadUploadSelectedFile(objStream, "First File")
                Uploader.CurrentSession.SelectedFiles.Add(uploadFile)
            End Using
     
            objBytes = System.Text.Encoding.GetEncoding("UTF-8").GetBytes("This is file2")
            Using objStream As New System.IO.MemoryStream(objBytes)
                Dim uploadFile As New RadUploadSelectedFile(objStream, "Second File")
                Uploader.CurrentSession.SelectedFiles.Add(uploadFile)
            End Using
     
    Uploader.PrepareSelectedFilesForUpload()
            If Uploader.CurrentSession.TotalFilesCount > 0 Then
                Uploader.StartUpload()
            End If

    This would work the first time without issues, "First File" and "Second File" would be written to the server.

    When I ran it again, I got an Access Denied error on "Second File".
    I tried with just "First File" - no problem.
    I tried just "Second File" - no problem.
    I tried both files again - Access denied on "Second File".
    I tried "Second" then "First" - Access denied on "First File".
    I added a "Third File" - Access denied on "Second" and "Third".

    All the files could be written the very first time, but only the first file in the list would succeed on any overwrite attempts.

    So, in my handler I added an override of every single RadUploadHandler base method so I could trace what each file was doing.
    It turns out that the first file uploaded will overwrite the existing file without first calling RadUploadHandler::RemoveFile() but subsequent files DO call it.  If you only have Read/Write permissions on the folder, RemoveFile will fail so the file upload fails.  Since the first file does not call RemoveFile it works just fine.

    I worked around it by giving delete permission on the folder, but this seems unnecessary since I don't actually want to delete files, I just want to upload them.  It also seems strange that the first file does not have this requirement.

    Can you explain the reason for this?


    Thanks.



  2. Alex Fidanov
    Admin
    Alex Fidanov avatar
    636 posts

    Posted 02 Apr 2012 Link to this post

    Hi,

     Overwriting existing files means that if a file with the same name already exists on the server, it will be overwritten. This is implemented by removing the file first and then saving it as a new file. The first time it works correctly, because there is no file with the same name on the server and the delete file code is never executed. However, the second time, the handler detects the already existing file and tries to delete it. For this to be working, you would need delete permissions in this folder.

    Another option is to override the RemoveFile method and clear the contents of the file, rather than physically deleting it. This should not throw the access exception.

    Greetings,
    Alex Fidanov
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  3. DevCraft banner
  4. Stephen
    Stephen avatar
    86 posts
    Member since:
    Jan 2011

    Posted 02 Apr 2012 Link to this post

    I think you have misunderstood what I find inconsistent:

    I realize that the first time I run the test, the files do not exist and there is no need to do a RemoveFile.
    The problem is that the second time I run the test(with the files already there), the first file uploaded does *not* do a RemoveFile call but subsequent files do call RemoveFile.  That is the inconsistency that confuses me.
  5. Alex Fidanov
    Admin
    Alex Fidanov avatar
    636 posts

    Posted 05 Apr 2012 Link to this post

    Hi,

     I am not able to reproduce this on my end (testing on Windows 7). The remove file method in the upload handler is always called for each file that already exists on the server.. However, I think this behavior might be environment specific - like OS, Security settings or antivirus software. Another source of the issue might be the size of the uploading files and the buffer size. How big are the files that you are uploading and are you using the default buffer size? Can you reproduce this behavior on another machine as well?

    Greetings,
    Alex Fidanov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  6. Pradeep
    Pradeep avatar
    6 posts
    Member since:
    Jun 2012

    Posted 03 Jul 2012 Link to this post

    Hi, I am facing one more issue related to OverwriteExistingFiles  property.

    I do first try to prompt the user before allowing him to overwrite the file of same name. but the Overwrite existing file property does not works in the code behind, below is the code which i am trying to get


            private void RadUpload1_FileUploadStarting(object sender, FileUploadStartingEventArgs e)
            {
                e.FileParameters.Add("EntityName", _entityName);
                e.FileParameters.Add("Key", _key);


                if (_docs.SourceCollection.OfType < IJVLinkFileModel>().Any(doc => doc.Name == e.SelectedFile.Name))
                {
                    if (MessageBox.Show("File " + e.SelectedFile.Name + " Already exists. Click OK to Continue or Cancel to Abort", "Alert", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
                        RadUpload1.OverwriteExistingFiles = false;
    else
    RadUpload1.OverwriteExistingFiles = true;   
                }    
            }


    Any suggestions appreciated !!

    Thanks
    Pradeep
  7. Alex Fidanov
    Admin
    Alex Fidanov avatar
    636 posts

    Posted 04 Jul 2012 Link to this post

    Hi Pradeep,

     It's probably because you are setting this property after the upload has started. Please note that this value is passed to the server, where the files are actually overwritten. I would recommend either setting this property before the upload has started uploading or moving this logic to the RadUploadHandler and handling this scenario there. For example, you can override SaveChunkData method, OverwriteExistingFiles property or InitializeChunkStorage method (where the OverwriteExistingFiles is used to determine whether the file has to be overwritten). 

    All the best,
    Alex Fidanov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  8. Pradeep
    Pradeep avatar
    6 posts
    Member since:
    Jun 2012

    Posted 09 Jul 2012 Link to this post

    Hi Alex, 
    Thanks for your response !! and you are right... it worked :)
Back to Top
DevCraft banner