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

Duplicate Upload - Stopping process

1 Answer 43 Views
Upload (Obsolete)
This is a migrated thread and some comments may be shown as answers.
Joe
Top achievements
Rank 2
Joe asked on 08 Dec 2008, 09:34 PM

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...

Imports Telerik.Web.UI     
Imports System.IO     
Imports System.Data.SqlClient     
Imports Telerik.Web.UI.Upload     
    
Protected Sub RadUpload1_FileExists(ByVal sender As ObjectByVal 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 ObjectByVal 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

1 Answer, 1 is accepted

Sort by
0
Joe
Top achievements
Rank 2
answered on 09 Dec 2008, 04:15 PM
Update:

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

Imports Telerik.Web.UI  
Imports System.IO  
Imports System.Data.SqlClient  
Imports Telerik.Web.UI.Upload  
 
Protected Sub SubmitButton_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles SubmitButton.Click  
        'Set target directory path to user's directory  
        'RadUpload1.TargetPhysicalFolder = Session("userDir")  
 
        If RadUpload1.UploadedFiles.Count > 0 Then 
            System.Threading.Thread.Sleep(3000)  
        End If 
 
        For Each f As UploadedFile In RadUpload1.UploadedFiles  
 
            Dim targetFolder As String = Session("UserDir")  
            Dim targetFileName As String = System.IO.Path.Combine(targetFolder, f.GetName)  
 
            If System.IO.File.Exists(targetFileName) Then 
                'Sets error label to visable and displays message.  
                lblUploadError1.Visible = True 
                lblUploadError1.Text = "File name " & f.GetName & " already exists!" 
            Else 
                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  
            End If 
        Next 
                RadUpload1.Enabled = False 
                SubmitButton.Enabled = False 
                Session("UploadUserID") = "" 
 
    End Sub 
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
Tags
Upload (Obsolete)
Asked by
Joe
Top achievements
Rank 2
Answers by
Joe
Top achievements
Rank 2
Share this question
or