Prevent overwrite files with RadAsyncUpload

4 posts, 0 answers
  1. Suzy
    Suzy avatar
    66 posts
    Member since:
    Nov 2014

    Posted 06 Mar 2015 Link to this post

    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
  2. Ivan Danchev
    Admin
    Ivan Danchev avatar
    829 posts

    Posted 10 Mar 2015 Link to this post

    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.

     
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Suzy
    Suzy avatar
    66 posts
    Member since:
    Nov 2014

    Posted 11 Mar 2015 in reply to Ivan Danchev Link to this post

    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

  5. Ivan Danchev
    Admin
    Ivan Danchev avatar
    829 posts

    Posted 13 Mar 2015 Link to this post

    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.

     
Back to Top