A
A
airwave2018-11-30 13:25:26
Django
airwave, 2018-11-30 13:25:26

How to properly upload files and bind them to an entity?

Hello!
Can you tell me how to upload files? and what is the best way to do it?
The bottom line is this: the user wants to create some kind of post, enters the post creation form, there are file uploaders in the form. When the user selects a file, the file should be uploaded to the server immediately, then the user fills in the rest of the fields and sends a POST request to the server.
How to link the files on the server with the entity of this post? The files are already on the server, but the post itself has not yet been created.
I see two options, but they seem to be crutch:
1. Create a post when the form is opened and return its id, link this id to files on the front and send files to the server with the link, and when saving the form, do PUT.
2. Upload files to a temporary folder, and when saving a post, compare file names and username and use them to bind to the post and move from the temporary folder where necessary.
What is the best way to implement this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Demian Smith, 2018-11-30
@airwave

Upload the file to a temporary folder and at the same time give the file a random unique name. During fasting, pass a bunch of real_name - random_name.
A random unique name should be given by the server and returned as a post response.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question