469,572 Members | 1,324 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,572 developers. It's quick & easy.

File Encoding Styles

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
Nov 17 '05 #1
8 7359
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.

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
"Xarky" <be*********@yahoo.com> wrote in message
news:bc*************************@posting.google.co m...
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


Nov 17 '05 #2
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

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #3
On Thu, 14 Apr 2005 13:22:33 +0300, Dmytro Lapshyn [MVP]
<x-****@no-spam-please.hotpop.com> wrote:
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".

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #4
Dmytro Lapshyn [MVP] <x-****@no-spam-please.hotpop.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5
xarky d_best <be*********@yahoo.com> wrote:
How should I decode the incoming data to bytes.


The easiest thing would be to use Convert.FromBase64String on the
returned text data.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6
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

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #7
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.

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
"xarky d_best" <be*********@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***


Nov 17 '05 #8
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.


*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Alexander Bolotnov | last post: by
12 posts views Thread by Brian Henry | last post: by
3 posts views Thread by Chip | last post: by
1 post views Thread by ujjwaltrivedi | last post: by
14 posts views Thread by Ashutosh Bhawasinka | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.