0

I am encoding an image to a base64 string using powershell. The encoding is working ok as I have testing it using

<img src="data:image/JPEG;charset=utf-8;base64, <base64string>">

Now I am attempting to update an SQL Server 2012 table with the base64 string using the following update statement:

update images set ImageBlob = convert(varbinary(MAX),<base64string>)

I have also tried to cast it to varbinary

update images set ImageBlob = cast(<base64string>, as varbinary(max))

When the update script runs I check the field value and the value is completely different than what i expected. The image will also not render on my page at all.

Am i doing something wrong when I attempt to load it into my database?

Andrew Burns
  • 346
  • 4
  • 15
  • Is there a reason not to use varchar(max)? – William Jones Jul 16 '16 at 12:33
  • I assumed saving them as varbinary was best for performance and database capacity? If it doesn't matter I will store it as varchar(max) which I have done and it works fine. – Andrew Burns Jul 16 '16 at 16:00
  • 1
    I don't believe there will be any different retrieving varchar versus varbinary, You could do a quick benchmark but to the best of my knowledge it won't make a difference... The other option is not to encode the image and store it as binary in a varbinary. If you need to you can encode/decode as here : http://stackoverflow.com/questions/5082345/base64-encoding-in-sql-server-2005-t-sql – William Jones Jul 16 '16 at 17:11

0 Answers0