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

Upload file in ItemCommand does not overwrite existing file

2 Answers 90 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Alexander
Top achievements
Rank 1
Alexander asked on 17 Apr 2012, 10:47 AM
Hi,
I am using an RadAsyncUpload inside my grid, to upload a single file. When in edit mode, the existing file is supposed to be overwritten. But this is not the case. I have tried with several different ways to create the file (my example here is the last one) - I have also tried using File.WriteAllBytes. This is my code:
In create mode, the file is created perfectly...
Control asyncUpload1 = editableItem.EditManager.GetColumnEditor("FileNameWithExtension").ContainerControl.FindControl("AsyncUpload1");
if (asyncUpload1 != null && asyncUpload1 is RadAsyncUpload)
{
    if (((RadAsyncUpload)asyncUpload1).UploadedFiles.Count > 0)
    {
        UploadedFile uploadedFile = ((RadAsyncUpload)asyncUpload1).UploadedFiles[0];
        if (letterTemplate.FileNameWithExtension != uploadedFile.FileName && letterTemplate.FileNameWithExtension != null)
        {
            File.Delete(GetTemplatePath(letterTemplate.MunicipalityCode, letterTemplate.FileNameWithExtension));
        }
 
        letterTemplate.FileNameWithExtension = uploadedFile.FileName;
 
        byte[] buffer = new byte[uploadedFile.InputStream.Length + 1];
        uploadedFile.InputStream.Read(buffer, 0, buffer.Length);
 
        CreateDirectoryIfNotExistent(Server.MapPath(string.Format(TEMPLATE_PATH, letterTemplate.MunicipalityCode)));
        using (FileStream stream = new FileStream(GetTemplatePath(letterTemplate.MunicipalityCode, letterTemplate.FileNameWithExtension), FileMode.Create))
        {
            stream.Write(buffer, 0, buffer.Length);
        }
    }
    else if (e.CommandName == CMD_CREATE)
    {
        Label error = (Label)editableItem.EditManager.GetColumnEditor("FileNameWithExtension").ContainerControl.FindControl("lblError");
        error.Text = "Angiv en Word skabelon-fil.";
        return;
    }
}

2 Answers, 1 is accepted

Sort by
0
Alexander
Top achievements
Rank 1
answered on 25 Apr 2012, 11:08 AM
Anybody got any idea why this is not working?
0
Maria Ilieva
Telerik team
answered on 30 Apr 2012, 10:54 AM
Hello Alexander,

I would suggest you to review the online demo below which present proper approach for achieving the required scenario:
http://demos.telerik.com/aspnet-ajax/controls/examples/integration/gridandradasyncupload/defaultcs.aspx?product=upload

Test it on your end and verify of this works for you.

Regards,
Maria Ilieva
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Alexander
Top achievements
Rank 1
Answers by
Alexander
Top achievements
Rank 1
Maria Ilieva
Telerik team
Share this question
or