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

Hash File Names before upload

1 Answer 272 Views
AsyncUpload
This is a migrated thread and some comments may be shown as answers.
Jugoslav
Top achievements
Rank 1
Jugoslav asked on 04 Jul 2016, 08:20 AM

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

1 Answer, 1 is accepted

Sort by
0
Accepted
Veselin Tsvetanov
Telerik team
answered on 06 Jul 2016, 01:33 PM
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.
Tags
AsyncUpload
Asked by
Jugoslav
Top achievements
Rank 1
Answers by
Veselin Tsvetanov
Telerik team
Share this question
or