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

Prevent overwrite files with RadAsyncUpload

3 Answers 212 Views
AsyncUpload
This is a migrated thread and some comments may be shown as answers.
Suzy
Top achievements
Rank 2
Suzy asked on 06 Mar 2015, 09:22 AM
Hi,

how can I prevent files being overwritten in the target folder? 

<telerik:RadAsyncUpload runat="server" ID="txtAddFile" MultipleFileSelection="Disabled" AllowedFileExtensions="csv" />


Kind regards

Suzy

3 Answers, 1 is accepted

Sort by
0
Ivan Danchev
Telerik team
answered on 10 Mar 2015, 01:07 PM
Hello,

By default the RadAsyncUpload overwrites the file with the same name in the Target folder. To remedy this you can subscribe to the control's FileUploaded event and insert your custom logic for naming the newly added files in its handler. The following code snippet shows how to do that and as an example uses a random generator to generate a number that is added to the name of the new file if another file with the same name exists in the Target folder:

protected void RadAsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
{
    string targetFolder = "UploadTarget";
 
    string targetFileName = Path.Combine(Server.MapPath(targetFolder), e.File.GetName());
    if (!File.Exists(targetFileName))
    {
        e.File.SaveAs(targetFileName);
    }
    else
    {
        Random rand = new Random();
        string randomNum = rand.Next(999, 9999).ToString();
 
        e.File.SaveAs(Path.Combine(Server.MapPath(targetFolder), randomNum + e.File.GetName()));
    }
}


Regards,
Ivan Danchev
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Suzy
Top achievements
Rank 2
answered on 11 Mar 2015, 08:22 AM
Hi Ivan,

Thank you, I can save the file now with another name. But when I want to know what the saved name was I cannot get it.

The ASyncUpload is used in the ItemCommand of a datagrid. The code below shows how I get the filename, but the filename I get here is the original filename.  I need the saved filename to save it to my database record that is created in the ItemCommand.

foreach (UploadedFile file in postedFile.UploadedFiles)
{
  filename = file.FileName;
  fileextension = file.GetExtension();
}

Any ideas?

Suzy

0
Ivan Danchev
Telerik team
answered on 13 Mar 2015, 04:40 PM
Hello,

You can store the uploaded file name into a variable:
protected void RadAsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
{
    string targetFolder = "images";
 
    string targetFileName = Path.Combine(Server.MapPath(targetFolder), e.File.GetName());
    if (!File.Exists(targetFileName))
    {
        e.File.SaveAs(targetFileName);
    }
    else
    {
        Random rand = new Random();
        string randomNum = rand.Next(999, 9999).ToString();
 
        e.File.SaveAs(Path.Combine(Server.MapPath(targetFolder), randomNum + e.File.GetName()));
        string savedFileName = randomNum + e.File.GetName();
    }
}

So in the example above the savedFileName variable, holds the name of the uploaded file with its extension, "3082MyImage.png" for instance.

Regards,
Ivan Danchev
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
AsyncUpload
Asked by
Suzy
Top achievements
Rank 2
Answers by
Ivan Danchev
Telerik team
Suzy
Top achievements
Rank 2
Share this question
or