locked
Cannot create a file when that file already exists. RRS feed

  • Question

  • User1773532935 posted

    I'm having an issue with this following bit of code. When I upload a file I want it to overwrite the existing file is there is one, but i'm getting an error when I try to do it.

    Protected Sub upBulkUpload_FileUploaded(ByVal sender As System.Object, ByVal args As CuteEditor.UploaderEventArgs) Handles upUpload.FileUploaded
            Dim strPath = Server.MapPath("/files/bustours/itinerary/")
            Dim filename As String = args.FileName 
            Dim strFilePath = strPath & filename
            args.MoveTo(strFilePath)
            Dim fileUpl As New IO.FileInfo(strFilePath)
            If Not fileUpl Is Nothing Then
                SM.ConfirmationMessage = "Your itinerary document has been uploaded"
                SM.RedirectText = "Back to Bus Tours"
                SM.RedirectURL = "/bus-tours/"
                ASM.RedirectToConfirmationPage()
            Else
                SM.ConfirmationMessage = "Error uploading your itinerary document. Please go back and try again"
                SM.RedirectText = "Back"
                SM.RedirectURL = "/bus-tours/admin/itinerary.aspx"
                ASM.RedirectToConfirmationPage()
            End If
            args.Delete()
    
        End Sub

    Cannot create a file when that file already exists.
    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
    at System.IO.File.Move(String sourceFileName, String destFileName)
    at CuteEditor.UploaderProvider.MoveTo(Guid fileguid, String newpath)
    at CuteEditor.UploaderEventArgs.MoveTo(String newpath)
    at MFTInstallSet.MFTWeb.Itinerary.upBulkUpload_FileUploaded(Object sender, UploaderEventArgs args)


    Thursday, November 21, 2013 11:27 AM

Answers

  • User2103319870 posted

    Hi,

    One suggestion is to use the System.IO.File class methods and check if the file exists in the particular location and if file exists delete the file and then execute the move operation

    Protected Sub upBulkUpload_FileUploaded(ByVal sender As System.Object, ByVal args As CuteEditor.UploaderEventArgs) Handles upUpload.FileUploaded
            Dim strPath = Server.MapPath("/files/bustours/itinerary/")
            Dim filename As String = args.FileName ''= MFT.WebFunctions.FileFunctions.GetAvailableFileName(args.FileName, strPath)
            Dim strFilePath = strPath & filename
            'Check if the file exists
            If System.IO.File.Exists(strFilePath) Then
                'Delete the Existing file
                System.IO.File.Delete(strFilePath)
            End If
            'Execute the Move to operation
                args.MoveTo(strFilePath)
                Dim fileUpl As New IO.FileInfo(strFilePath)
                If Not fileUpl Is Nothing Then
                    SM.ConfirmationMessage = "Your itinerary document has been uploaded"
                    SM.RedirectText = "Back to Bus Tours"
                    SM.RedirectURL = "/bus-tours/"
                    ASM.RedirectToConfirmationPage()
                Else
                    SM.ConfirmationMessage = "Error uploading your itinerary document. Please go back and try again"
                    SM.RedirectText = "Back"
                    SM.RedirectURL = "/bus-tours/admin/itinerary.aspx"
                    ASM.RedirectToConfirmationPage()
                End If
                args.Delete()
    
        End Sub

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 21, 2013 11:42 AM

All replies

  • User2103319870 posted

    Hi,

    One suggestion is to use the System.IO.File class methods and check if the file exists in the particular location and if file exists delete the file and then execute the move operation

    Protected Sub upBulkUpload_FileUploaded(ByVal sender As System.Object, ByVal args As CuteEditor.UploaderEventArgs) Handles upUpload.FileUploaded
            Dim strPath = Server.MapPath("/files/bustours/itinerary/")
            Dim filename As String = args.FileName ''= MFT.WebFunctions.FileFunctions.GetAvailableFileName(args.FileName, strPath)
            Dim strFilePath = strPath & filename
            'Check if the file exists
            If System.IO.File.Exists(strFilePath) Then
                'Delete the Existing file
                System.IO.File.Delete(strFilePath)
            End If
            'Execute the Move to operation
                args.MoveTo(strFilePath)
                Dim fileUpl As New IO.FileInfo(strFilePath)
                If Not fileUpl Is Nothing Then
                    SM.ConfirmationMessage = "Your itinerary document has been uploaded"
                    SM.RedirectText = "Back to Bus Tours"
                    SM.RedirectURL = "/bus-tours/"
                    ASM.RedirectToConfirmationPage()
                Else
                    SM.ConfirmationMessage = "Error uploading your itinerary document. Please go back and try again"
                    SM.RedirectText = "Back"
                    SM.RedirectURL = "/bus-tours/admin/itinerary.aspx"
                    ASM.RedirectToConfirmationPage()
                End If
                args.Delete()
    
        End Sub

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 21, 2013 11:42 AM
  • User1773532935 posted

    Works perfectly!

    Thanks for your help!

    Thursday, November 21, 2013 11:52 AM