How to save a Base64 image to SQL and save it locally as PNG

Asked

Viewed 2,025 times

5

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 download in all browsers this difficult so I wonder if it is possible and easier using SQL? The idea would be to use a precedent.

  • 1

    Without understanding your environment and use case is very complicated. In the case of SQL Server read on FILESTREAM

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 type fields depending on your database.

To download you need to revert it to binary format, otherwise the file will look corrupted. You can do this in the 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 storing it directly in the file system?

0

Well, saving an image in an SQL database is complicated because its table structure is rigid, and when compressing, 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 maybe in some cloud storage service) and only put the address of the image in an SQL table field.
Another alternative is to use a database noSQL, such as mongoDB, in which its structure is flexible, i.e., 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

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

  1. In case 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 already mentioned in a comment Filestream.

Here’s a link with some explanations: MSFT Sample Images

Browser other questions tagged

You are not signed in. Login or sign up in order to post.