How to change the uploaded file name?

Oct 12, 2010 at 11:55 AM


How can I change the uploaded file name?

I know I can do this in HttpUploadHandler.ashx , but then I can't access uploaded file names with Content.Files.FileList[n].FileName, because obviously it contains the original name.

The goal here is to have my own names at the first save to disk, letting it save under original name and changing it later is not acceptable. Any other way?

Oct 12, 2010 at 12:12 PM

The upload handler is "dumb" and only is saving bytes for 1 file. It does not know anything about the Silverlight side.

How do you want to determine the final name? The HttpUploadHandler.ashx is open source, so you can modify it if you want.

Oct 12, 2010 at 12:45 PM

The final name is a combination of values for user ID, particular upload origin, data type plus a random hash. All provided through the view controller, so I guess I could pass it on to the control through CustomParams, and changing the name is not a problem in the .ashx then. But still, I can't use the control's FileList then, for a reason you mentioned.

Maybe there is a way to modify the naming behaviour in the control's .cs source? Or I could do the naming in the .ashx, but then would need a way to get the new filenames back to the view.

I don't want to implement the naming logic (ie provide all the needed values and build my own filelist) on the view itself, which as far as I know is the only way to do this if the above are not possible.

Oct 12, 2010 at 12:52 PM

It's a little more clear now. Maybe the best option for you is to change the Silverlight source code of the Multi File Uploader. You can then change the name of the File object yourself and it gets uploaded with the right filename.

Oct 12, 2010 at 1:18 PM

Done. Not really that difficult, too.

AddFile in MainPage.xaml.cs:

        private void AddFile(FileInfo file)
            string fileName = file.Name;

            //Create a new UserFile object
            UserFile userFile = new UserFile();
            userFile.FileName = file.Name;
            userFile.FileStream = file.OpenRead();

            //Check for the file size limit (configurable)
            if (userFile.FileStream.Length <= Configuration.Instance.MaxFileSize)
                //Add to the list
 >>>>>          userFile.FileName = mpost.SilverlightMultiFileUpload.Core.Configuration.Instance.CustomParams;

                ((IVisualizeFileRow)_fileRow).UserFile = userFile;
                ((FrameworkElement)_fileRow).Visibility = System.Windows.Visibility.Visible;
                if (MaximumFileSizeReached != null)
                    MaximumFileSizeReached(this, null);


This works out fine for now.

Thanks for pointing me in the right direction!