How to save a base64 image in SQL and save it locally as PNG

I need to know if it is possible to save a base64 image in SQL and then retrieve it and save it locally as PNG or JPEG. I already know how to convert to base64, but to perform the download in all browsers this difficult so I would like to know if it is possible and easier using SQL? The idea would be to use a procedure.

Author: Almir Antonio, 2016-03-01

3 answers

Possible is, if you convert it to base64, you can store in text fields that fit your content.

You can also store it in binary format using blob/BINARY type fields depending on your bank.

To perform the download you need to revert it to binary format, otherwise the file will appear corrupted. You can do this in the script itself responsible for making it available to the user.

Now the real question is: why chose to store it in the database, rather than saving it directly in the file system?

 1
Author: Antonio Antunes, 2017-02-01 05:26:36

Well, saving an image in a SQL database is complicated because its table structure is rigid, and when compression is done, the size varies depending on the complexity of the image. If you use an SQL database, the best solution is to store the image separately(or perhaps in some cloud storage service) and put only the address of the image in a field in the SQL table.
Another alternative is to use a noSQL bank, such as mongoDB, in which its structure is flexible, that is, the size of the field is defined by delimiters, not by allocation. But, it is more complicated to make the relationship between the collections (corresponds to the tables in SQL), since this is not relational.

 0
Author: David Jesse, 2016-03-25 02:15:28

If you really want to do this storage in your database you can use one of two options:

  1. If you are sure that the image will not have more than 2GB stores with Varbinary (max)
  2. If this space can happen to be exceeded then it starts to use what they already mentioned in a comment the Filestream.

Here is a link with some explanations: MSFT Sample Images

 0
Author: WizardDBA, 2016-05-09 08:37:24