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

Telerik RadAsyncUpload control - Rename multiple files names if already exist

2 Answers 294 Views
Upload (Obsolete)
This is a migrated thread and some comments may be shown as answers.
Praveen kumar
Top achievements
Rank 1
Praveen kumar asked on 17 Jun 2015, 07:12 PM

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 Answers, 1 is accepted

Sort by
0
Accepted
Bozhidar
Telerik team
answered on 19 Jun 2015, 03:22 PM
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
0
Praveen kumar
Top achievements
Rank 1
answered on 19 Jun 2015, 09:32 PM
Thanks alot....for suggestions. really i was not thinking in other way.
Tags
Upload (Obsolete)
Asked by
Praveen kumar
Top achievements
Rank 1
Answers by
Bozhidar
Telerik team
Praveen kumar
Top achievements
Rank 1
Share this question
or