Telerik RadAsyncUpload control - Rename multiple files names if already exist

Thread is closed for posting
3 posts, 1 answers
  1. Praveen kumar
    Praveen kumar avatar
    11 posts
    Member since:
    Jun 2015

    Posted 17 Jun 2015 Link to this post

    Telerik RadAsyncUpload control is used upload files to file system or shared folder. if file already exist we need to append counter value to the end of the file.So i wrote logic to added integer value to the end of  file name. This code works with single file but if upload multiples, this code fails .I want to rename multiple files if already exist in file share.

     protected void RadAsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
            {
               
                List<ListItem> files = new List<ListItem>();
                int counter = 1;
                foreach (UploadedFile file in AsyncUpload1.UploadedFiles)
                {
                    
                    string targetFolder = AsyncUpload1.TargetFolder;
                    string targetFileName = System.IO.Path.Combine(targetFolder,
                        file.GetNameWithoutExtension() + counter.ToString() + file.GetExtension());
                    while (System.IO.File.Exists(targetFileName))
                    {
                        counter++;
                        targetFileName = System.IO.Path.Combine(targetFolder,
                            file.GetNameWithoutExtension() + counter.ToString() + file.GetExtension());
                        
                    }
               
                   file.SaveAs(targetFileName);
                    
                }

  2. Answer
    Bozhidar
    Admin
    Bozhidar avatar
    1103 posts

    Posted 19 Jun 2015 Link to this post

    Hello,

    The FileUploaded event is fired for every uploaded file individually, so you must not iterate through the files manually. Instead you can get a reference to each file with the e.File property.
    protected void RadAsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
    {
        List<ListItem> files = new List<ListItem>();
        int counter = 1;
     
        var file = e.File;
     
        string targetFolder = Server.MapPath(RadAsyncUpload1.TargetFolder);
        string targetFileName = System.IO.Path.Combine(targetFolder,
            file.GetNameWithoutExtension() + counter.ToString() + file.GetExtension());
        while (System.IO.File.Exists(targetFileName))
        {
            counter++;
            targetFileName = System.IO.Path.Combine(targetFolder,
                file.GetNameWithoutExtension() + counter.ToString() + file.GetExtension());
        }
     
        file.SaveAs(targetFileName);
    }


    Regards,
    Bozhidar
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Praveen kumar
    Praveen kumar avatar
    11 posts
    Member since:
    Jun 2015

    Posted 19 Jun 2015 in reply to Bozhidar Link to this post

    Thanks alot....for suggestions. really i was not thinking in other way.
Back to Top