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

manipulating file name before saving to disk

1 Answer 90 Views
Upload
This is a migrated thread and some comments may be shown as answers.
Patrick
Top achievements
Rank 1
Patrick asked on 17 Jan 2010, 09:56 PM
I would like to remove the underscore and make sure the file name is lowercase before the file is saved to the disk. Something like this

 

//Returns the file name and extension of the specified path string.

 

 

string fileName = System.IO.Path.GetFileName(FileUploadAddPhotoToPost.FileName);

 

 

string fileNameRemoveHTMLEncoding = HttpUtility.UrlDecode(fileName);

 

 

string fileNameReplaceToLower = fileNameRemoveHTMLEncoding.Replace(" ", "_").ToLower();

 

 

//Assembles entire physical path(including filename) to store images.

 

 

string fullPath = System.IO.Path.Combine(physicalFilePath, fileNameReplaceToLower);

 


Where do I have to put this code to accomplish this? ProceesStream seems to late, GetTargetFolder seems the wrong place?

1 Answer, 1 is accepted

Sort by
0
Ivan
Telerik team
answered on 18 Jan 2010, 12:41 PM
Hello Patrick,

Thank you for contacting us.

Actually there are two places you can change the name of the uploaded file:
  • Client-size
    You should handle the FileUploadStarting event and initialize the FileName property of the upload-structure (this approach is demonstrated in the How to upload a file with a different name article):
     
    void RadUpload_FileUploadStarting(object sender,
        Telerik.Windows.Controls.FileUploadStartingEventArgs e)
    {
        // construct a new file name:
        string newFileName = "CustomFileName.txt";
     
        // initialize the upload-struxture:
        e.UploadData.FileName = newFileName;
    }
     
  • Server-side
    You should override the GetFilePath and GetTargetFolder(this approach is demonstrated in the Customize the Upload Storage and the Saving files with unique name on the server using RadUploadHandler articles):
     
    public override string GetFilePath(string fileName)
    {
        fileName = this.GenerateNewFileName(fileName);
        return base.GetFilePath(fileName);
    }
     
    public virtual string GetTargetFolder()
    {
        string folder = base.GetTargetFolder();
     
        // modify the folder name...
     
        return folder;
    }
     
Please preview these articles (and try their examples) and let us know if you need further assistance.
 
All the best,
Ivan
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.
Tags
Upload
Asked by
Patrick
Top achievements
Rank 1
Answers by
Ivan
Telerik team
Share this question
or