It appears that the RadUpload1_FileExists fires twice. I have an extremely simple page with just the RAdUpload on it (and Scriptmanager). I am using you sample code (below) to test for existence of the file. If it exists a single Upload filed will place two files into the upload folder. Example: I have a Test.txt already in the folder from previous upload. Then I try to upload it egain and I get Test1.txt and Test2.txt uploaded with single click of the submit button.
Any ideas?
Thanks
Jakub
Protected Sub RadUpload1_FileExists(ByVal sender As Object, ByVal e As Telerik.Web.UI.Upload.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
7 Answers, 1 is accepted
I tested the scenario you have, however, I was not able to reproduce the problem you experience. For your convenience I have attached my test page. Please, take a look at it and let me know whether your scenario differs.
In case the attached page cannot help you solve the problem, send me a simple running application which reproduces the problem including all the needed files for testing it. I will review it and will do my best to help you. For this purpose you will need to open a support ticket so that you will be able to attach files. I have attached a screenshot which shows how to do this.
Best regards,
Sophy
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
OnFileExists="RadUpload1_FileExists"
in the control, as indicated in your sample application and help file. You need to update them.
I removed it and it works fine now.
Thanks,
Jakub
I am glad the problem is solved. In case you find wrong examples in any of our resources - documentation, online demos, knowledge base, code library projects, etc. please send us the links and we will update them as soon as possible.
Best wishes,
Petya
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
There is additional issue with renaming the files. I need to bew able to save them into database, but seem to have acces to the original names only (see code below).
How can I access the renamed files (and addtional Input fileds that I added to the Upload control)
Thanks,
Jakub
Protected Sub RadUpload1_FileExists(ByVal sender As Object, ByVal e As Telerik.Web.UI.Upload.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
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim txt As String = ""
If RadUpload1.UploadedFiles.Count > 0 Then
For Each f As UploadedFile In RadUpload1.UploadedFiles
Title = f.GetFieldValue(
"Title")
txt = txt +
"Title: " + Title + " - " + f.GetName + ", "
'Database insert here
Next
Me.Label1.Text = txt
Else
End If
End Sub
1. In your particular scenario I suggest that you rename the files, save them to disc, and then save them to DB in one and the same method to avoid the naming problem. For example you can do the following:
- do not handle FileExists
- in Button1_Click for each uploaded file you can perform a check whether it exists with File.Exists() and depending on the result perform the actions you do now in RadUpload1_FileExists. Then in the same method you can save the file information to DB as you now have the new names.
- Please, note that you should not specify
TargetFolder in this scenario - otherwise you will end up with the
files saved twice - once with SaveAs and once by the automatic saving
to the target folder.
Hope we have been helpful.
Greetings,
Petya
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
That worked great.
actually the TargetFolder needs to be there in order for the routine to know where to put the files. It does not upload twice. see code below
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim txt As String = ""
Dim iSQL As String
Dim Title As String
Dim DocDate As String
If RadUpload1.UploadedFiles.Count > 0 Then
For Each f As UploadedFile In RadUpload1.UploadedFiles
'***/Make File unique
Dim counter As Integer = 1
Dim targetFolder As String = Server.MapPath(RadUpload1.TargetFolder)
Dim targetFileName As String = System.IO.Path.Combine(targetFolder, f.GetNameWithoutExtension & counter & f.GetExtension)
While System.IO.File.Exists(targetFileName)
counter += 1
targetFileName = System.IO.Path.Combine(targetFolder, f.GetNameWithoutExtension & counter & f.GetExtension)
End While
f.SaveAs(targetFileName)
'***/Make File unique
Title = f.GetFieldValue(
"DocName")
If IsDate(f.GetFieldValue("DocDate")) Then
DocDate = f.GetFieldValue(
"DocDate")
Else
DocDate =
""
End If
txt = txt +
"Title: " + Title + " - " + f.GetNameWithoutExtension & counter & f.GetExtension + ", " + f.ContentType + ", TTTTitle: " + Me.txtTitle.Text
'Database insert
iSQL =
"INSERT INTO teFile (FileName, DocName, DocDate, Category, Site, MIMEtype) VALUES ('" + targetFileName + "', '" + f.GetFieldValue("DocName") + "', CONVERT(DATETIME, '" + DocDate + "', 102), '" + f.GetFieldValue("Category") + "', '" + f.GetFieldValue("Site") + "', '" + f.ContentType + "')"
Dim cmd As New System.Data.SqlClient.SqlCommand
cmd.CommandType = CommandType.Text
cmd.CommandText = iSQL
cmd.Connection = SQLDbCon
SQLDbCon.Open()
Dim NoxRec As Integer
NoxRec = cmd.ExecuteNonQuery()
SQLDbCon.Close()
Next
Me.lblFilesLoaded.Text = txt
Else
End If
End Sub
As far as I tested your code the file does get uploaded twice only with a different name. For example if I am uploading a file with the name "Test.txt" and it does not exist in the target folder before that, after upload I will end up with "Test.txt" and "Test1.txt" in the target folder - the first from the automatic upload enabled by specifying the TargetFolder property and the second from the f.SaveAs method. I decided to mention this in case you have missed it.
Best wishes,
Petya
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center