Crash Course 101
10 modules
5 weeks

Files in database and server requests

Click to copy

How to send file to server and save it in database

After all checks, we can be sure that the user has submitted files in the correct format. Now is the time to send them to the server for further processing. But first, it is worth making an important explanation about the principles of working with files.

Types of files

You may have noticed that there are two different types of files. The first one is the one we just worked with, the model file. The second is the file data type. It is an ID that is easy to store in the database and easy to find the complete model. When designing a database model, it is the second type that is used. It can be used in the same way as other data types (String, Integer, and others), but at the same time, we always know that the full model is hidden behind its ID.

When we get a file from the Filepicker or the Select Files block, it is represented as a model. But it doesn't have an ID yet, because the file hasn't been saved in the server's database. To do this, you need to make the appropriate Server request POST /_files/ request to the endpoint, which was automatically created in advance. After this, the file is written to the database, and its model acquires an ID and can be fully used.

URL for images

Use Swagger to see what other file endpoints exist. We need one of them right now. To use files as images in the front-end, you need to get the full URL to the given file. This task is solved by the GET /_files/:id/download/ endpoint. Knowing the file ID, you can use it and get the exact file URL.

We need to expand the model obtained as a result of the Server request POST /_files/ block and find out the file ID. After that, the ID must be turned into a string using the To String block. Let's use this result to collect the final image URL (the same as GET request /_files/:id/download/). To do this, we use the Concat Strings (Multiple) string concatenation block. The first part will be "/api/_files/", the second will be the file ID, and the third will be "/download/".

It remains only to pass the resulting URL to the Image block. It was created at the design stage, but initially there was a simple stub as an image. Now we can fix it. To do this, use the Image Update Properties block and set the URL to the image.

Was this article helpful?
Still looking for an answer?
Join the Community