manipulating file name before saving to disk

2 posts, 0 answers
  1. Patrick
    Patrick avatar
    17 posts
    Member since:
    May 2009

    Posted 17 Jan 2010 Link to this post

    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?
  2. Ivan
    Ivan avatar
    519 posts

    Posted 18 Jan 2010 Link to this post

    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,
    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.
Back to Top