Duplicate Upload - Stopping process

Thread is closed for posting
2 posts, 0 answers
  1. Joe
    Joe avatar
    51 posts
    Member since:
    Oct 2008

    Posted 08 Dec 2008 Link to this post

    I am trying to code an upload page that does the following:

    1. User selects up to 5 files to upload
    2. 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...

    1 Imports Telerik.Web.UI     
    2 Imports System.IO     
    3 Imports System.Data.SqlClient     
    4 Imports Telerik.Web.UI.Upload     
    5     
    6 Protected Sub RadUpload1_FileExists(ByVal sender As ObjectByVal e As UploadedFileEventArgs) Handles RadUpload1.FileExists     
    7         Dim file As UploadedFile = e.UploadedFile  
    8         'Session("UserDir") comes from other working code   
    9         Dim targetFolder As String = Session("UserDir")     
    10         Dim targetFileName As String = System.IO.Path.Combine(targetFolder, file.GetExtension)     
    11           
    12         While System.IO.File.Exists(targetFileName)     
    13             'Sets error label to visable and displays message.     
    14             lblUploadError.Visible = True    
    15             lblUploadError.Text = "File name already exists!"    
    16         End While    
    17 End Sub    
    18  
    19 Protected Sub SubmitButton_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles SubmitButton.Click     
    20    
    21         If RadUpload1.UploadedFiles.Count > 0 Then    
    22             System.Threading.Thread.Sleep(3000)     
    23         End If    
    24     
    25         For Each f As UploadedFile In RadUpload1.UploadedFiles     
    26             f.SaveAs(Session("UserDir") & f.GetName, False)     
    27     
    28             ' Insert a new record into Call_Info table     
    29     
    30             'Set Active Date on new user to today     
    31             Dim UploadDate As DateTime = DateTime.Now     
    32     
    33             Dim connectionString As String = ConfigurationManager.ConnectionStrings("eonPortalCS").ConnectionString     
    34             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)"    
    35             Dim myConnection As New System.Data.SqlClient.SqlConnection(connectionString)     
    36             Dim myCommand As New System.Data.SqlClient.SqlCommand(insertSql, myConnection)     
    37               
    38                 Using myConnection     
    39                 myConnection.Open()     
    40                 myCommand.Parameters.AddWithValue("@UserId", Session("UploadUserID"))     
    41                 myCommand.Parameters.AddWithValue("@FileName", f.GetName)     
    42                 myCommand.Parameters.AddWithValue("@FileLocation", Session("UserDir"))     
    43                 myCommand.Parameters.AddWithValue("@Discipline", Session("UserDiscipline"))     
    44                 myCommand.Parameters.AddWithValue("@CallStatus""New")     
    45                 myCommand.Parameters.AddWithValue("@UploadDate", UploadDate)     
    46                 myCommand.Parameters.AddWithValue("@UploadedBy", Session("CurrentUserGuid"))     
    47                 myCommand.ExecuteNonQuery()     
    48                 myConnection.Close()     
    49             End Using     
    50         Next    
    51         RadUpload1.Enabled = False    
    52         SubmitButton.Enabled = False    
    53         Session("UploadUserID") = ""    
    54     End Sub    
    55  

    Any suggestions?

    Thanks,
    Joe

  2. Joe
    Joe avatar
    51 posts
    Member since:
    Oct 2008

    Posted 09 Dec 2008 Link to this post

    Update:

    I have solved the duplicate upload problem with the following code:

    1 Imports Telerik.Web.UI  
    2 Imports System.IO  
    3 Imports System.Data.SqlClient  
    4 Imports Telerik.Web.UI.Upload  
    5  
    6 Protected Sub SubmitButton_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles SubmitButton.Click  
    7         'Set target directory path to user's directory  
    8         'RadUpload1.TargetPhysicalFolder = Session("userDir")  
    9  
    10         If RadUpload1.UploadedFiles.Count > 0 Then 
    11             System.Threading.Thread.Sleep(3000)  
    12         End If 
    13  
    14         For Each f As UploadedFile In RadUpload1.UploadedFiles  
    15  
    16             Dim targetFolder As String = Session("UserDir")  
    17             Dim targetFileName As String = System.IO.Path.Combine(targetFolder, f.GetName)  
    18  
    19             If System.IO.File.Exists(targetFileName) Then 
    20                 'Sets error label to visable and displays message.  
    21                 lblUploadError1.Visible = True 
    22                 lblUploadError1.Text = "File name " & f.GetName & " already exists!" 
    23             Else 
    24                 f.SaveAs(Session("UserDir") & f.GetName, False)  
    25  
    26                 ' Insert a new record into Call_Info table  
    27  
    28                 'Set Active Date on new user to today  
    29                 Dim UploadDate As DateTime = DateTime.Now  
    30  
    31                 Dim connectionString As String = ConfigurationManager.ConnectionStrings("eonPortalCS").ConnectionString  
    32                 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)" 
    33                 Dim myConnection As New System.Data.SqlClient.SqlConnection(connectionString)  
    34                 Dim myCommand As New System.Data.SqlClient.SqlCommand(insertSql, myConnection)  
    35                 Using myConnection  
    36                     myConnection.Open()  
    37                     myCommand.Parameters.AddWithValue("@UserId", Session("UploadUserID"))  
    38                     myCommand.Parameters.AddWithValue("@FileName", f.GetName)  
    39                     myCommand.Parameters.AddWithValue("@FileLocation", Session("UserDir"))  
    40                     myCommand.Parameters.AddWithValue("@Discipline", Session("UserDiscipline"))  
    41                     myCommand.Parameters.AddWithValue("@CallStatus""New")  
    42                     myCommand.Parameters.AddWithValue("@UploadDate", UploadDate)  
    43                     myCommand.Parameters.AddWithValue("@UploadedBy", Session("CurrentUserGuid"))  
    44                     myCommand.ExecuteNonQuery()  
    45                     myConnection.Close()  
    46                 End Using  
    47             End If 
    48         Next 
    49                 RadUpload1.Enabled = False 
    50                 SubmitButton.Enabled = False 
    51                 Session("UploadUserID") = "" 
    52  
    53     End Sub 
    54 End Class 

    The only issue I know have is how to step thru the f.GetName and write a custom error message to a label to warn the user that the file already exists.

    I've started a new thread on this problem.

    Thanks,
    Joe
Back to Top