Upload saving initial file twice

Thread is closed for posting
4 posts, 0 answers
  1. Web Services
    Web Services avatar
    243 posts
    Member since:
    Apr 2008

    Posted 14 Nov 2011 Link to this post

    Whenever I run this script, if the file doesn't exist, it saves it twice. If that file name does exist, then it only saves it once. Any ideas why?

    Protected Sub submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submit.Click
            Dim targetFileName
            fileOutput.Text = ""
     
            Dim counter = 1
            Dim targetFolder As String = Server.MapPath(attachedFile.TargetFolder)
     
            'check the file upload
            If (attachedFile.UploadedFiles.Count > 0) Then
     
                For Each postedFile As UploadedFile In attachedFile.UploadedFiles
                    If Not [Object].Equals(postedFile, Nothing) Then
                        targetFileName = Path.Combine(targetFolder, postedFile.GetNameWithoutExtension() + counter.ToString() + postedFile.GetExtension())
                        While System.IO.File.Exists(targetFileName)
                            counter += 1
                            targetFileName = Path.Combine(targetFolder, postedFile.GetNameWithoutExtension() + counter.ToString() + postedFile.GetExtension())
                        End While
                    End If 'if not object
                    postedFile.SaveAs(targetFileName)
                Next 'for each uploaded file
     
            Else
     
            End If 'if attachedFile.UploadedFiles.Count
     
    End Sub
  2. Genady Sergeev
    Admin
    Genady Sergeev avatar
    1596 posts

    Posted 17 Nov 2011 Link to this post

    Hi,

    Remove the

    postedFile.SaveAs(targetFileName)

    line from your code. It causes the duplicate saving. When there is TargetFolder set on the upload, it will automatically save the files there unless there is file exists name space collision.


    Regards,
    Genady Sergeev
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Web Services
    Web Services avatar
    243 posts
    Member since:
    Apr 2008

    Posted 18 Nov 2011 Link to this post

    I updated my code, but now if the file exists, it doesn't save a copy with the increment on the end

    'check the file upload
            If (attachedFile.UploadedFiles.Count > 0) Then
     
                For Each postedFile As UploadedFile In attachedFile.UploadedFiles
                    If Not [Object].Equals(postedFile, Nothing) Then
                        targetFileName = Path.Combine(targetFolder, postedFile.GetNameWithoutExtension() + counter.ToString() + postedFile.GetExtension())
                        While System.IO.File.Exists(targetFileName)
                            counter += 1
                            targetFileName = Path.Combine(targetFolder, postedFile.GetNameWithoutExtension() + counter.ToString() + postedFile.GetExtension())
                        End While
                    End If 'if not object
                    'postedFile.SaveAs(targetFileName)
                Next 'for each uploaded file
     
            Else
     
            End If 'if attachedFile.UploadedFiles.Count
  5. Kevin
    Kevin avatar
    360 posts
    Member since:
    Jul 2012

    Posted 21 Nov 2011 Link to this post

    You need to change your code to this:

    'check the file upload
            If (attachedFile.UploadedFiles.Count > 0) Then
       
                For Each postedFile As UploadedFile In attachedFile.UploadedFiles
                    If Not [Object].Equals(postedFile, Nothing) Then
                        targetFileName = Path.Combine(targetFolder, postedFile.GetNameWithoutExtension() + counter.ToString() + postedFile.GetExtension())
                        If System.IO.File.Exists(targetFileName) Then
                            counter += 1
                            targetFileName = Path.Combine(targetFolder, postedFile.GetNameWithoutExtension() + counter.ToString() + postedFile.GetExtension())
                            postedFile.SaveAs(targetFileName)
                        End If
                    End If 'if not object
                    'postedFile.SaveAs(targetFileName)
                Next 'for each uploaded file
       
            Else
       
            End If 'if attachedFile.UploadedFiles.Count

    You need to call the postedFile.SaveAs line where you check if the file exists. I removed the While loop since it makes more sense to use an If statement to check if a file exists.

    I hope that helps.
Back to Top