I am trying to code an upload page that does the following:
- User selects up to 5 files to upload
- Once they click submit, behind code validates that none of the (1 to 5) uploaded files does not already exsist.
a. If they do, then an error message stating which file is a duplicate is displayed to the user on the postback.
b. If all files (1 to 5) do not exist, then they are saved to server, the SQL table is updated with the new file names, locations, etc.
I have the upload section working well as well as the writing the new file information to the correct table but I cannot get the file exists check code to work properly.
Here's what I have...
Imports Telerik.Web.UI | |
Imports System.IO | |
Imports System.Data.SqlClient | |
Imports Telerik.Web.UI.Upload | |
Protected Sub RadUpload1_FileExists(ByVal sender As Object, ByVal e As UploadedFileEventArgs) Handles RadUpload1.FileExists | |
Dim file As UploadedFile = e.UploadedFile | |
'Session("UserDir") comes from other working code | |
Dim targetFolder As String = Session("UserDir") | |
Dim targetFileName As String = System.IO.Path.Combine(targetFolder, file.GetExtension) | |
While System.IO.File.Exists(targetFileName) | |
'Sets error label to visable and displays message. | |
lblUploadError.Visible = True | |
lblUploadError.Text = "File name already exists!" | |
End While | |
End Sub | |
Protected Sub SubmitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SubmitButton.Click | |
If RadUpload1.UploadedFiles.Count > 0 Then | |
System.Threading.Thread.Sleep(3000) | |
End If | |
For Each f As UploadedFile In RadUpload1.UploadedFiles | |
f.SaveAs(Session("UserDir") & f.GetName, False) | |
' Insert a new record into Call_Info table | |
'Set Active Date on new user to today | |
Dim UploadDate As DateTime = DateTime.Now | |
Dim connectionString As String = ConfigurationManager.ConnectionStrings("eonPortalCS").ConnectionString | |
Dim insertSql As String = "INSERT INTO Call_Info(UserId, File_Name, File_Location, Discipline, Call_Status, Upload_Date, Uploaded_By) VALUES(@UserId, @FileName, @FileLocation, @Discipline, @CallStatus, @UploadDate, @UploadedBy)" | |
Dim myConnection As New System.Data.SqlClient.SqlConnection(connectionString) | |
Dim myCommand As New System.Data.SqlClient.SqlCommand(insertSql, myConnection) | |
Using myConnection | |
myConnection.Open() | |
myCommand.Parameters.AddWithValue("@UserId", Session("UploadUserID")) | |
myCommand.Parameters.AddWithValue("@FileName", f.GetName) | |
myCommand.Parameters.AddWithValue("@FileLocation", Session("UserDir")) | |
myCommand.Parameters.AddWithValue("@Discipline", Session("UserDiscipline")) | |
myCommand.Parameters.AddWithValue("@CallStatus", "New") | |
myCommand.Parameters.AddWithValue("@UploadDate", UploadDate) | |
myCommand.Parameters.AddWithValue("@UploadedBy", Session("CurrentUserGuid")) | |
myCommand.ExecuteNonQuery() | |
myConnection.Close() | |
End Using | |
Next | |
RadUpload1.Enabled = False | |
SubmitButton.Enabled = False | |
Session("UploadUserID") = "" | |
End Sub | |
Any suggestions?
Thanks,
Joe