Developer forum

Forum » CMS - Standard features » Alter/change filename on upload through forms for editors

Alter/change filename on upload through forms for editors

Hans Ravnsfjall
Hans Ravnsfjall
Reply

Hi

 

Is there someway to alter/change the filename when uploading through Forms for editors?

We would like to add eg. a DateTime.Now to the filename. THe new filename ofcourse will have to be a the link that will be shown in the email sent when the form is filled.

Is this possible somehow?

/Hans


Replies

 
Lars Larsen
Lars Larsen
Reply

Hi Hans

Why do you want to add a DateTime.Now to the filename of an uploaded file? All uploaded files will have a creation datetimestamp. If you want this information in a confirmation email you can get this information in a Razor template using System.IO.GetCreationTime(). 

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

The reason for adding timestamp to the photos is, that the files are being transferred to a local drive daily, and deleted from the dynamicweb folder. If two files have the same filename eg. image1.jpg, then the file on the local folder will be overwritten, which will cause havoc. If we rename the file on the local folder on the fly, then the problem will be with the email sent along with the form submittance, as the local link in the mail will be broken - since the filename is changed after being uploaded.

Therefore I was thinking of adding a timestamp to the filename, to ensure a unique filename. Another posibility would be to replace the filename totally with a 32 digit random string or something like that.

therefore I need to change the filename on upload.

Any ideas how this can be achieved?

/hans

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Anyone who can tell me if this would be possible to achieve?

 

/Hans

 
Kristian Kirkholt Dynamicweb Employee
Kristian Kirkholt
Reply

Hi Hans

It is not an option by standard DW to change the filename and adding timestamp during uploade. But perhaps this could be achieved by advanced template/Scripting.

Kind Regards
Dynamicweb Support
Kristian Kirkholt

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

@Kristian, will it be enough to alter the value on the filefield before uploading the file, or does this have to be done "while/after" uploading the file? 

 

/Hans

 
Nicolai Pedersen
Reply

No, that would not be possible either. Then no file would upload.

So only option is to catch the upload after it is done...

The first notification after file is saved is Notifications.Frontend.OnAfterSubmitSave

BR Nicolai

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Hi

 

The Need for a fix on this has appeard again. Nicolai, anything more you can say to guide me in a direction of getting a solution for this?

Can you elaborate how I can use the notification to alter the filenames?

 

/Hans

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Could i Also do like this?

When the file is uploadad, and right before i write the link to the file in the email template, I will change the name of the file on the server and alter the link in the email template accordingly?

 

Something link this?

 

 

@{
string dateext=DateTime.Now.ToString("yyyyMMddHHmmssffff");
string fileName = "Files\FormUpload\image.jpg";
string newFileName = fileName.Substring(0, fileName.LastIndexOf('.')) + @dateext + fileName.Substring(fileName.LastIndexOf('.'));

File.Copy(fileName, newFileName);
File.Delete(fileName);
}

 

Would this work?

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

And here I am again. This wonΒ΄t go away untill I find a solution for it.

Before I spend hours going down an impossible path, anyone who by chance can tell me if it would work to alter the filename from the mail template that sends the form information?

Either by the example above, or by using System.IO.File.Move, to "move" the file to a new name like this?

System.IO.File.Move("oldfilename", "newfilename");

Anyone?

Would really appreciate it.

 
Nicolai Pedersen
Reply

It looks reasonable. But hard to tell without trying. Would go for the move version. Also be aware that the FormSubmitData table holds the information about the original upload path.

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

ok, thank you

will have a go at it πŸ‘πŸ»

/Hans

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Have implemented it, and can confirm that It works πŸ‘πŸ»

 

/Hans

 

You must be logged in to post in the forum