This is a migrated thread and some comments may be shown as answers.

Upload saving initial file twice

3 Answers 82 Views
Upload (Obsolete)
This is a migrated thread and some comments may be shown as answers.
Web Services
Top achievements
Rank 2
Web Services asked on 14 Nov 2011, 10:25 PM
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

3 Answers, 1 is accepted

Sort by
0
Genady Sergeev
Telerik team
answered on 17 Nov 2011, 12:26 PM
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
0
Web Services
Top achievements
Rank 2
answered on 18 Nov 2011, 05:32 PM
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
0
Kevin
Top achievements
Rank 2
answered on 21 Nov 2011, 02:34 PM

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.
Tags
Upload (Obsolete)
Asked by
Web Services
Top achievements
Rank 2
Answers by
Genady Sergeev
Telerik team
Web Services
Top achievements
Rank 2
Kevin
Top achievements
Rank 2
Share this question
or