File Encoding Styles

  • Thread starter Thread starter Xarky
  • Start date Start date
X

Xarky

Hi,
I am downloading a GIF file(as a mail attachement) with this file
format, Content-Transfer-Encoding: base64;

Now I am writing the downloaded data to a file with this technique:

streamWriter = new StreamWriter(@startupPath+"\\"+filename, false);
streamWriter.WriteLine(data);

I am not specifying any file Encoding. When I try to open the file
just created with Microsoft Photo Editor, the following error is
given: "Can't determine type.";

I have tried this technique with different file formats, but it worked
only for text-files.

Can someone give some help on how to solve this problem.
Thanks in Advance
 
Hi,

First of all, you need to decode the incoming base64 stream to get the
actual GIF file bytes.
Once you get the byte array, you will need to use the BinaryWriter class and
its Write method to write out the resultant array to a file.

A tip for recognizing whether the byte array is actually a GIF file - it
should contain the 'GIF89a' signature (can also be 'GIF87' but that one is
really rare) somewhere in the beginning.
 
Hi,

How should I decode the incoming data to bytes.

Can you please give me some futher help or recommend me some links.

Thanks in Advance
 
Hi,

First of all, you need to decode the incoming base64 stream to get the
actual GIF file bytes.
Once you get the byte array, you will need to use the BinaryWriter class
and its Write method to write out the resultant array to a file.

A tip for recognizing whether the byte array is actually a GIF file - it
should contain the 'GIF89a' signature (can also be 'GIF87' but that one
is really rare) somewhere in the beginning.

Yep, the first three bytes in a gif file is always "GIF" and the following
three bytes is the version number and can be "89a" or "87a".
 
Dmytro Lapshyn said:
First of all, you need to decode the incoming base64 stream to get the
actual GIF file bytes.
Once you get the byte array, you will need to use the BinaryWriter class and
its Write method to write out the resultant array to a file.

Note that there's no real reason to use BinaryWriter here - a straight
stream would do everything required.
 
xarky d_best said:
How should I decode the incoming data to bytes.

The easiest thing would be to use Convert.FromBase64String on the
returned text data.
 
Hi,
After doing some debugging I kept track for the following information:

Text File:
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable
This worked perfectly with writing the data being read with a
streamwriter.

GIF:
Content-Type: image/gif
Content-Transfer-Encoding: base64
As suggested converted to base64 and used BinaryWriter, and worked
perfect

Excel:
Content-Type: application/vnd.ms-excel
Content-Transfer-Encoding: base64
As suggested converted to base64 and used BinaryWriter, and worked
perfect

MP3:
Content-Type: audio/mpeg
Content-Transfer-Encoding: base64
As suggested converted to base64 and used BinaryWriter, and worked
perfect

PDF:
Content-Type: application/pdf
Content-Transfer-Encoding: quoted-printable
Tried in both ways (like Text-files, and MP3/GIF/etc) both this file
didn't work and gave an error when tried to open file.
Error given is the following: "There was an error opening this document.
The file is damaged and could not be repaired"

Then I tried another PDF file:
Content-Type: application/pdf
Content-Transfer-Encoding: base64
As suggested converted to base64 and used BinaryWriter, and worked
perfect.

What could the problem be. Could it be that the pdf file is corrupted,
though the original file opens perfectly.

Can someone help me out.
Thanks in Advance
 
Hi,

Pay attention to this header:

Content-Transfer-Encoding: quoted-printable

It is NOT base64, nor these are plain bytes. This is yet another type of
encoding, please search the Web for its specifics.
 
Hi,
I am trying searching on the web on how to encode and how to write
"quoted-printable" data.

Can some help be provided, because I can't find anything useful.

Thanks in Advance.
 
Back
Top