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

5

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

    
asked by anonymous 01.03.2016 / 15:29

3 answers

1

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

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

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

Now the real question is: why did you choose to store it in the database, instead of saving it directly to the file system?

    
01.02.2017 / 06:26
0

Well, saving an image to 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 a SQL database, the best solution is to store the image separately (or perhaps to some cloud storage service) and just put its address in a field of the SQL table. Another alternative is to use a noSQL database, such as mongoDB, in which its structure is flexible, that is, the size of the field is defined by delimiters rather than by allocation. But it is more complicated to do the relationship between the collections (it corresponds to the tables in SQL), since this is not relational.

    
25.03.2016 / 02:45
0

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

  • If you are sure that the image will not have more than 2GB stores with Varbinary (max)
  • If this space can be exceeded then use the one already mentioned in a Filestream comment.
  • Here you have a link with some explanations: MSFT Sample Images

    / p>     
    09.05.2016 / 10:37