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

FileExists

1 Answer 61 Views
Upload (Obsolete)
This is a migrated thread and some comments may be shown as answers.
Marnix Bouwman
Top achievements
Rank 2
Marnix Bouwman asked on 04 Apr 2011, 10:27 AM
Hi, I'm using Radupload and trying to save a record to the database like so on a button click event:

If RadUpload1.UploadedFiles.Count > 0 Then
        .Picture = RadUpload1.UploadedFiles(0).FileName
Else
        .Picture = ""
End If

However, I would also like to change the filename when the file already exists. So I added the FileExists function:
        Protected Sub RadUpload1_FileExists(ByVal sender As Object, ByVal e As UploadedFileEventArgs) Handles RadUpload1.FileExists
            Dim counter As Integer = 1
            Dim file As UploadedFile = e.UploadedFile
            Dim targetFolder As String = Server.MapPath(RadUpload1.TargetFolder)
            Dim targetFileName As String = System.IO.Path.Combine(targetFolder, file.GetNameWithoutExtension & counter & file.GetExtension)
            While System.IO.File.Exists(targetFileName)
                counter += 1
                targetFileName = System.IO.Path.Combine(targetFolder, file.GetNameWithoutExtension & counter & file.GetExtension)
            End While
            file.SaveAs(targetFileName)
        End Sub

This results in changing the filename to picture1.jpg if picture.jpg already exists.
However, it will still save picture.jpg to the database.
How can I make sure it will be updated in the correct way?

1 Answer, 1 is accepted

Sort by
0
Dimitar Terziev
Telerik team
answered on 07 Apr 2011, 12:13 PM
Hi Marnix,

In general this FileExist event is fired when you have either TargetFolder or TargetPhysicalFolder set and OverwriteExistingFiles property is set to False.

If you save your files to a database, this event won't prevent files with duplicating names to be saved. This event is used only when the files are saved on the file system.

Since you haven't provided the code used to insert records to a database, my suggestion is the following. In this button click event handler function you will check, if there is already a file with such name and if so you will change the name of the currently uploaded file manually and then save it to the database.

Best wishes,
Dimitar Terziev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Upload (Obsolete)
Asked by
Marnix Bouwman
Top achievements
Rank 2
Answers by
Dimitar Terziev
Telerik team
Share this question
or