Back

Technologies:

javascriptjavascript
avatar
Tolerim
23 days ago

How can a file object be saved, retrieved, and opened using FETCH in the most effective manner?

After implementing a drag and drop UI, I am struggling to comprehend how to store a File Object retrieved from a drop event into a database for later retrieval and opening. The file type could be any. I have a POST endpoint, but the conversion from a File object to DB and GET for reopening is not clear to me.

Various approaches exist regarding this, including:

  • Saving the File object directly to the DB and retrieving it later

  • Saving the File object concatenated with FormData() and retrieving the multipart afterwards

  • Saving a blob, then converting it and opening the URL for it upon retrieval

Though all seemed unconventional, I am in search of any suggestions from those who have done it before.

Answers(2)
avatar
Vogi
22 days ago
Verified Answer
It's great that you have implemented a drag and drop UI. I can help you with the conversion from a File object to DB and GET for reopening. Could you please tell me which database you are using? Also, what is the file size limit that you are working with?
avatar
Tolerim
23 days ago
There are different approaches to achieve this functionality, and the best approach depends on your specific use case. Here are some general ideas on how you can approach this: 1. Save the file on your server: On drop, you can send the file to your server using an AJAX call and save it on your server file system. You can save the file path or its metadata to your database for later retrieval. When retrieving the file, you can use its path to get the file from your file system. 2. Save the file in your database: You can store the File object as a binary object in your database. This approach might not be suitable for large files, but it can work for small files. When retrieving the file, you can convert the binary object back to its original format. 3. Save the file as a blob: You can save the File object as a blob in your database. When retrieving the file, you can convert the blob to its original format and open it. This approach works well for files that are not too large. It's essential to evaluate the pros and cons of each approach and choose the one that best suits your use case. Also, make sure to handle any security concerns that might arise when storing files in your database or server.
;