Exception: Cannot create a file when that file already exists.

Last post 08-31-2010, 9:47 PM by aperezgzlz. 2 replies.
Sort Posts: Previous Next
  •  08-30-2010, 10:32 AM 63725

    Exception: Cannot create a file when that file already exists.

    Hi guys,
     
    I have a problem when I try to call method MoveTo to move all the uploaded files to our directory.
    1. void Uploader_FileUploaded(object sender, UploaderEventArgs args)    
    2. {    
    3.      args.MoveTo(Server.MapPath("~/Uploads/"));    
    4. }   
    Here is the message of that exception:

    Cannot create a file when that file already exists.

    I need your advice on this.
    Thanks in advance!
     
    Regards,
    Tien
    Filed under:
  •  08-30-2010, 11:39 AM 63730 in reply to 63725

    Re: Exception: Cannot create a file when that file already exists.

    Dear tien1504,
     
    <script runat="server">
    void Uploader_FileUploaded(object sender, UploaderEventArgs args)   
    {
        string path = "c:\\temp\\" + args.FileName;       
        //check whether same name file is existing
        if (System.IO.File.Exists(path))
        {
            //add your own code here, you can return directly, you can also change file name
        }       
        args.MoveTo(path);
    }
    </script>
     
    Thanks for asking
  •  08-31-2010, 9:47 PM 63772 in reply to 63725

    Re: Exception: Cannot create a file when that file already exists.

    Try this:
     

    Protected Sub Uploader1_FileUploaded(ByVal sender As Object, ByVal args As CuteWebUI.UploaderEventArgs) Handles Uploader1.FileUploaded

    Dim fileExt As String = System.IO.Path.GetExtension(args.FileName).ToUpper

    If (fileExt = ".GIF") Or (fileExt = ".JPG") Or (fileExt = ".JPEG") Then

    Dim saveDir As String = "pictures\"

    Dim appPath = Request.PhysicalApplicationPath

    Dim savePath As String = appPath + saveDir

    'Obtener el nombre del archivo.

    Dim fileName As String = args.FileName

    'Crear una ruta y nombre para checar si hay archivos duplicados

    Dim pathToCheck As String = savePath + fileName

    'Crear un nombre temporal para checar duplicados

    Dim tempfileName As String

    'Checar si existe un archivo con el mismo nombre que el que se va a subir

    If (System.IO.File.Exists(pathToCheck)) Then

    Dim counter As Integer = 1

    While (System.IO.File.Exists(pathToCheck))

    ' Si existe un archivo con el mismo nombre, le agrega un contador numerico al principio

    tempfileName = counter.ToString() + fileName

    pathToCheck = savePath + tempfileName

    counter = counter + 1

    End While

    fileName = tempfileName

    End If

    ' Agregar el nombre a la ruta fisica

    savePath += fileName

    'Guardar el archivo

    args.CopyTo(savePath)

    Dim InsertSql As String = "INSERT INTO pictures (picture) VALUES ?picture)"

    Dim cmd As New MySqlCommand(InsertSql, conn)

    Try

    conn.open()

    With cmd

    .CommandType = CommandType.Text

    .Parameters.AddWithValue("?picture", fileName)

    .Prepare()

    .ExecuteNonQuery()

    End With

    Catch ex As Exception

    Errores(ex.Message)

    Finally

    If Not conn Is Nothing Then
    conn.close()
    End If

    End Try

    End If

    End Sub

View as RSS news feed in XML