0

I have a table column with string data beginning with 0xFFD8FFE000.... and ending with FFD9. Each row is similar in start and end, but different between.

If I inspect the resulting HTML around the image, I learn that it displays as a PNG. My end goal is a scripted export that will convert the string to a file. I am looking for info, links included, to send me on my way.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Scottieie
  • 304
  • 8
  • 14
  • ff d8 is a jpeg not a png btw. There does no appear to be a question here, what has HTML got to do with anything? – Alex K. Mar 13 '17 at 16:37
  • @AlexK.Thanks for the comment. The question ending with a question mark doesn't exist, however this is a request for how to begin thinking about a problem. HTML inspection was my approach to understanding what and how the image stored as info. in a table becomes an image. PNG happened to be mentioned in the resulting set of HTML. My assumption could be incorrect, and humility isn't foreign to me...... – Scottieie Mar 14 '17 at 14:54

1 Answers1

2

You have a few options. All of them include some amount of coding. There isn't a built in way for SQL Server to export base64 encoded strings as files.

However you can....

Write a CLR that will do it from the TSQL / database side. The relevant C# code you would need in the CLR is something like this:

File.WriteAllBytes(@"c:\yourfile", Convert.FromBase64String(yourBase64String));

This is courtesy of this link: Base64 encoded string to file

Or write a quick console application to run through each of your rows and pull it out. Using the same code snippet above for C# or any other number of languages that are documented out on google. Another method a quick google search found is:

public Image Base64ToImage(string base64String)
{
  // Convert Base64 String to byte[]
  byte[] imageBytes = Convert.FromBase64String(base64String);
  MemoryStream ms = new MemoryStream(imageBytes, 0, 
    imageBytes.Length);

  // Convert byte[] to Image
  ms.Write(imageBytes, 0, imageBytes.Length);
  Image image = Image.FromStream(ms, true);
  return image;
}

Courtesy of here: http://www.dailycoding.com/Posts/convert_image_to_base64_string_and_base64_string_to_image.aspx

Community
  • 1
  • 1
RThomas
  • 10,702
  • 2
  • 48
  • 61
  • Thanks for the info. on how to begin approaching this and the great links. I gathered that there wasn't a stock method for exporting to files. The vendor (large HRIS) indicates it can take them in, but it can't give them out. A programmer i am not. R and SQL to analyze and interface data are my stock in trade, but can tie together odds and ends with enough background. You have sent me in the right direction. – Scottieie Mar 14 '17 at 15:10