Save files with filename=objectId of related object

I want to create files with names related to the object they are saved together with.

I guess I first have to save the file when the user uploads it, then upon form submit take the resulting objectId and rename the file to whatever I want as its name.

Is that the correct approach? Or is there a better/less tidious way of doing this?

Hi @Egil_Helland

It depends on your application requirements and logic.
In programming, there is not only one solution, one solution might be the only one for one app and completely wrong/unacceptable for another one =)

  • creating Data Objects and Saving/Uploading files is a different operation and you can not run them in a single API Call, which means there are always first and second requests.
  • if you can save a DataObject first you can predict a file path before uploading it and then having the objectId save the file with the required path.
  • using objectId in the Files as a reference is good idea, it might be in a file name or it can be a directory name, it’s up to you

Regards,
Vlad

I had the same requirement, and what I did was to generate a UUID, use it to save the file first. And then add the object to the database, using the UUID as the new objectId.

1 Like

I understand @vladimir-upirov.

In this case, I need to save the file first, as there are data input limits on the table object that prevents me from creating a record up front. Or, I guess I could create a record up front if I have a temporary status field of sorts and just give valid but unverified input data as part of the creation, and do an update afterwards. That is perhaps more in line with what you are thinking?

Smart @Nicolas_REMY! That seems like a brilliant idea. I’ll try out that approach :slight_smile:

1 Like