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

Can Not Upload One File at Third Times

2 Answers 57 Views
Upload (Obsolete)
This is a migrated thread and some comments may be shown as answers.
hu tao
Top achievements
Rank 1
hu tao asked on 13 Oct 2009, 04:59 AM
I am working in a project,I have used the RadUpload,the code of upload file in my project is follow:

protected void Submit_Click(object sender, EventArgs e)  
    {  
        if (RadUpload1.UploadedFiles.Count > 0)  
        {  
            System.Threading.Thread.Sleep(3000);  
            try 
            {  
                string targetFolder = Server.MapPath(@"UpLoadFile\Project");  
                string targetFileName = string.Empty;  
                foreach (UploadedFile files in RadUpload1.UploadedFiles)  
                {  
                    int counter = 1;  
                    string fileName = files.GetName();  
                    string fileName1 = files.GetNameWithoutExtension() + counter.ToString() + files.GetExtension();  
                    targetFileName = System.IO.Path.Combine(targetFolder, fileName);  
                    while (System.IO.File.Exists(targetFileName))  
                    {  
                        targetFileName = System.IO.Path.Combine(targetFolder, fileName1);  
                        fileName = fileName1;  
                        counter++;  
                    }  
                    files.SaveAs(targetFileName);  
                      
                    //here,I insert the file info into DB;                      
                }  
            }  
            catch (Exception ex)  
            {  
                throw ex;  
            }  
       }  
    }  
 

First,I upload a file "test.txt" into server, then I upload the file "test.txt" second times, in server it's name is renamed as "test1.txt",this project works well, but when I upload the file "test.txt" third times , the RadProgressArea's Elapsed time and Speed run all the time, the radupload can not upload this file, I try this action several times ,but can not work well too.
I think my code where rename the upload file can not work well,but it seems that my code is correct.
What can I do in this situation?
Are there any settings about RadUpload to solve my question?

Thanks
Hu Tao

2 Answers, 1 is accepted

Sort by
0
Accepted
Genady Sergeev
Telerik team
answered on 14 Oct 2009, 10:42 AM
Hello hu tao,

Your code is incorrect, please move the

string fileName1 = files.GetNameWithoutExtension() + counter.ToString() + files.GetExtension();

line inside the while loop. So, the correct version should look like this:


protected void Submit_Click(object sender, EventArgs e)
   {
       if (RadUpload1.UploadedFiles.Count > 0)
       {
           System.Threading.Thread.Sleep(3000);
           try
           {
               string targetFolder = Server.MapPath(@"UpLoadFile\Project");
               string targetFileName = string.Empty;
               foreach (UploadedFile files in RadUpload1.UploadedFiles)
               {
                   int counter = 1;
                   string fileName = files.GetName();
                    
                   targetFileName = System.IO.Path.Combine(targetFolder, fileName);
                   while (System.IO.File.Exists(targetFileName))
                   {
                       string fileName1 = files.GetNameWithoutExtension() + counter.ToString() + files.GetExtension();
                       targetFileName = System.IO.Path.Combine(targetFolder, fileName1);
                       fileName = fileName1;
                       counter++;
                   }
                   files.SaveAs(targetFileName);
 
                   //here,I insert the file info into DB;                     
               }
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }
   }

This should fix the problems you experience.

Sincerely yours,
Genady Sergeev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
hu tao
Top achievements
Rank 1
answered on 15 Oct 2009, 09:28 AM
Thanks,My problem has been solved with your help!
Tags
Upload (Obsolete)
Asked by
hu tao
Top achievements
Rank 1
Answers by
Genady Sergeev
Telerik team
hu tao
Top achievements
Rank 1
Share this question
or