Read Base64 string to write a .gif (or other content type)

  • Thread starter Thread starter AC [MVP MCMS]
  • Start date Start date
A

AC [MVP MCMS]

Any pointers on how to (1) read a Base64 encoded string from a text file
and (2) write it to a binary file?

I have a ton of files that are being generated from a legacy system. Each
file contains a (1) content type one one line, a (2) filename, and (3) the
encoding type (seems to always be Base64, followed by the encoded string.
A process will be to read the content type and filename, then create a new
binary file and write out the converted base64 string.

Ideas?
 
Hello James,

Thanks... I guess I should have been more specific. I have that part, converting
the string to a byte array. But for images, I've noticed that there's a
header at the start of the file (appears to be just plain text, but not sure)...
for example a gif has GIF[version] at the start of the file.

I've tried writing out "GIF89a" at the start and then the byte array, but
that didn't work. Then I tried writing the GIF89a to a separate byte array,
write that out and then write the data stream byte array... but that didn't
work. I've seen some examples in this NNTP about using the BinaryWriter,
and comments by Jon Skeet how that's not necessary, but having a hard time
finding a single example of how to write this out on your own.

I'm not only interested in writing out GIF's, but also BMPs, JPEG, PNGs,
ZIPs, DOCs, XLSs, etc...

Ideas?
 
Create a MemoryStream from the byte[]. Use the FromStream() and Save()
methods of System.Drawing.Image.
Hello James,

Thanks... I guess I should have been more specific. I have that
part, converting the string to a byte array. But for images, I've
noticed that there's a header at the start of the file (appears to be just
plain text, but
not sure)... for example a gif has GIF[version] at the start of the
file.
I've tried writing out "GIF89a" at the start and then the byte array,
but that didn't work. Then I tried writing the GIF89a to a separate byte
array, write that out and then write the data stream byte array...
but that didn't work. I've seen some examples in this NNTP about
using the BinaryWriter, and comments by Jon Skeet how that's not
necessary, but having a hard
time finding a single example of how to write this out on your own.

I'm not only interested in writing out GIF's, but also BMPs, JPEG,
PNGs, ZIPs, DOCs, XLSs, etc...

Ideas?

How 'bout System.Convert.FromBase64String ()

Home: www.noveltheory.com Work: www.njtheater.com Blog:
 
Hello James,

This will work for images... but will it work for other content types (ZIP,
DOC, XLS)?

--
-AC [MVP MCMS]
http://www.andrewconnell.com
http://www.andrewconnell.com/mvp
Create a MemoryStream from the byte[]. Use the FromStream() and
Save() methods of System.Drawing.Image.
Hello James,

Thanks... I guess I should have been more specific. I have that
part, converting the string to a byte array. But for images, I've
noticed that there's a header at the start of the file (appears to be
just
plain text, but
not sure)... for example a gif has GIF[version] at the start of the
file.
I've tried writing out "GIF89a" at the start and then the byte array,
but that didn't work. Then I tried writing the GIF89a to a separate
byte
array, write that out and then write the data stream byte array...
but that didn't work. I've seen some examples in this NNTP about
using the BinaryWriter, and comments by Jon Skeet how that's not
necessary, but having a hard
time finding a single example of how to write this out on your own.
I'm not only interested in writing out GIF's, but also BMPs, JPEG,
PNGs, ZIPs, DOCs, XLSs, etc...

Ideas?
How 'bout System.Convert.FromBase64String ()

Home: www.noveltheory.com Work: www.njtheater.com Blog:
www.honestillusion.com Day Job: www.partsearch.com "AC [MVP MCMS]"

Any pointers on how to (1) read a Base64 encoded string from a text
file and (2) write it to a binary file?

I have a ton of files that are being generated from a legacy
system. Each file contains a (1) content type one one line, a (2)
filename, and (3) the encoding type (seems to always be Base64,
followed by the encoded string. A process will be to read the
content type and filename, then create a new binary file and write
out the converted base64 string.

Ideas?
 
AC said:
This will work for images... but will it work for other content types
(ZIP, DOC, XLS)?

What exactly are you starting with? If the Base64 is a binary image of
the file you want write, why are you bothering with conversions at all.
Just write out the bits, and give it the appropriate extension.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top