Getting Final Name(s) Of Uploaded Files

Thread is closed for posting
2 posts, 0 answers
  1. Mick
    Mick avatar
    27 posts
    Member since:
    Mar 2008

    Posted 19 Jul 2008 Link to this post

    Hello, I have asked this question before, but didnt get the response I desired, maybe this was due to me being unclear on my intentions, so I shall try again.
    I am using the RadUpload control to allow the uploading of image files to my server. After the files a uploaded, they are minipulated server side, height, width etc are adjusted and a watermark is added, so it is very important that I have the correct filename after upload.
    When the upload control detects that a file already exists, it fires the following function:

    protected void RadUpload1_FileExists(object sender, UploadedFileEventArgs e)  
        {  
            int counter = 1;  
     
            UploadedFile file = e.UploadedFile;  
     
            string targetFolder = Server.MapPath(RadUpload1.TargetFolder);  
     
            string targetFileName = Path.Combine(targetFolder,  
                file.GetNameWithoutExtension() + counter.ToString() + file.GetExtension());  
     
            while (System.IO.File.Exists(targetFileName))  
            {  
                counter++;  
                targetFileName = Path.Combine(targetFolder,  
                    file.GetNameWithoutExtension() + counter.ToString() + file.GetExtension());  
            }  
     
            file.SaveAs(targetFileName);  
        }         
    This works well, the uploaded file is renanmed by appending an extra "1" to the filename, until a unique name is generated.
    However my problem is, if I try the followning in my upload button click event:
     protected void Button1_Click(object sender, EventArgs e)  
        {  
            foreach (UploadedFile f in RadUpload1.UploadedFiles)  
            {  
                if (!Object.Equals(f, null))  
                {  
                    LooongMethodWhichUpdatesTheProgressContext(f);  
                }  
            }  
            foreach (UploadedFile f in RadUpload1.UploadedFiles)  
            {  
                Response.Write(f.GetName() + "<br>");  
            }  
        }  
     
    I only get the original filenames, if the filename has been changed by the fileexists funtion above, the correct name is not shown in the uploaded files collection. I need a way to be able to access the filenames, as they are saved on the server. Yes I can save the filename of the modified file within the fileexists method to a string list, however the original file name still exists within the uploaded files collection.
  2. Veselin Vasilev
    Admin
    Veselin Vasilev avatar
    2992 posts

    Posted 22 Jul 2008 Link to this post

    Hello Mick,

    The UploadedFiles collection contains all valid uploaded files. The UploadedFile class has a FileName property which contains the name of the file on the client. Additionally, the GetName() method returns that name as well.
    You cannot modify the file name property in the collection.

    Therefore, you need to keep the actual file names in another place, e.g. in a string array - as you already supposed.

    I hope this helps.

    Sincerely yours,
    Veskoni
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
Back to Top