Hash File Names before upload

2 posts, 1 answers
  1. Jugoslav
    Jugoslav avatar
    64 posts
    Member since:
    Mar 2011

    Posted 04 Jul Link to this post

    I have set the TargetFolder e.g. 

    TargetFolder="~/content"

    and it works fine except that i want the file names to be kind od hashed e.g. myfile.jpg to be saved as 8unfsKjfjskskdjf^7i.jpg

    I have the HashFileName(string file) function but not quite sure where to apply it. 

     

    Thank you

  2. Answer
    Veselin Tsvetanov
    Admin
    Veselin Tsvetanov avatar
    287 posts

    Posted 06 Jul Link to this post

    Hello Jugoslav,

    You could handle the FileUploaded server side event in order to process the file, change its name and save it manually to your server:
    protected void Unnamed_FileUploaded(object sender, Telerik.Web.UI.FileUploadedEventArgs e)
    {
        // Get the file object
        var file = e.File;
        // Get file name and extension as string array
        var fileNameAndExtension = file.GetName().Split(new char[]{'.'});
        // Set a random string as file name
        fileNameAndExtension[0] = "RandomString";
        // Join new name with file extension
        var finalName = fileNameAndExtension[0] + "." + fileNameAndExtension[1];
        // Set pah to folder on server
        string path = Server.MapPath("~/Uploads/");
        // Save file with the new name in the given path
        file.SaveAs(path + finalName);
    }

    Regards,
    Veselin Tsvetanov
    Telerik by Progress
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top