469,290 Members | 1,913 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Base64 directly to file

I am getting a huge file attachment from SWA web service, obviously in
Base64. Is there a way to decode the Base64 string directly to a file
(FileStream or similar) instead to byte array?

Jan 18 '07 #1
6 24155
<se************@gmail.comwrote:
I am getting a huge file attachment from SWA web service, obviously in
Base64. Is there a way to decode the Base64 string directly to a file
(FileStream or similar) instead to byte array?
Use CryptoStream with FromBase64Transform. There's an example in the
FromBase64Transform description in MSDN which does pretty much what you
need.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 18 '07 #2
Hi,
I dont know about the directly decoding it to file, but you could read fixed
chunks in sizes which should be multiple of 4 (in a loop) and convert and
write them to the destination file (i have used the chunk size of 4):

something like the following code (its just using a textbox but u can use
filestream of course);

string base64text = textBox2.Text;
int startingindex = 0;
txttranslated.Clear(); // destination textbox
while (startingindex<base64text.Length)
{
string tmp = base64text.Substring(startingindex, 4);
startingindex += 4;
txttranslated.AppendText (Encoding.ASCII.GetString (
Convert.FromBase64String(tmp)));
}

hope that help ..
Regards,

...ab

<se************@gmail.comwrote in message
news:11**********************@v45g2000cwv.googlegr oups.com...
>I am getting a huge file attachment from SWA web service, obviously in
Base64. Is there a way to decode the Base64 string directly to a file
(FileStream or similar) instead to byte array?

Jan 18 '07 #3
Hi,

If you've got the byte[] from the base64 string then you
can just write it to the file e.g. by FileStream.

private static void Base64ToFile(string base64String, string dstFilePath) {
byte[] dataBuffer=Convert.FromBase64String(base64String);
using( FileStream fileStream=new FileStream(dstFilePath
, FileMode.Create
, FileAccess.Write) ) {
if( dataBuffer.Length>0 ) {
fileStream.Write(dataBuffer, 0, dataBuffer.Length);
}
}
}

HTH
Marcin

se************@gmail.com napisał(a):
I am getting a huge file attachment from SWA web service, obviously
in Base64. Is there a way to decode the Base64 string directly to a
file (FileStream or similar) instead to byte array?
Jan 18 '07 #4
Marcin Grzębski wrote:
If you've got the byte[] from the base64 string then you
can just write it to the file e.g. by FileStream.
I believe the OP's point is that they don't want to have another full
copy of the data in memory at the same time - and indeed they may not
even have a *single* copy of the data in memory, if they stream the
input. Doing everything in one shot could be very expensive in terms of
memory.

Jon

Jan 18 '07 #5
Hi Jon,

Jon Skeet [C# MVP] napisał(a):
Marcin Grzębski wrote:
>If you've got the byte[] from the base64 string then you
can just write it to the file e.g. by FileStream.

I believe the OP's point is that they don't want to have another full
copy of the data in memory at the same time - and indeed they may not
even have a *single* copy of the data in memory, if they stream the
input. Doing everything in one shot could be very expensive in terms of
memory.

Jon
You're right!
I missed the "huge file attachment" as an input.

Marcin
Jan 18 '07 #6
Thx you all for the responses

I have decided to go with Abubakar's suggestions and, for the future
reference, this is a simple function that does what I need
//--------------------------------------------------------------------------------
private string Base64Decode(string data, string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate,
FileAccess.Write);
int sizeOfChunk = 4;
int startPosition = 0;
while (startPosition < data.Length)
{
string tmp = data.Substring(startPosition, sizeOfChunk);
startPosition = startPosition + sizeOfChunk;
byte[] tmpArr = Convert.FromBase64String(tmp);
fs.Write(tmpArr, 0, tmpArr.Length);
}
fs.Close();
fs.Dispose();
return fileName;
}
//--------------------------------------------------------------------------------

Abubakar wrote:
Hi,
I dont know about the directly decoding it to file, but you could read fixed
chunks in sizes which should be multiple of 4 (in a loop) and convert and
write them to the destination file (i have used the chunk size of 4):

something like the following code (its just using a textbox but u can use
filestream of course);

string base64text = textBox2.Text;
int startingindex = 0;
txttranslated.Clear(); // destination textbox
while (startingindex<base64text.Length)
{
string tmp = base64text.Substring(startingindex, 4);
startingindex += 4;
txttranslated.AppendText (Encoding.ASCII.GetString (
Convert.FromBase64String(tmp)));
}

hope that help ..
Regards,

..ab

<se************@gmail.comwrote in message
news:11**********************@v45g2000cwv.googlegr oups.com...
I am getting a huge file attachment from SWA web service, obviously in
Base64. Is there a way to decode the Base64 string directly to a file
(FileStream or similar) instead to byte array?
Jan 18 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Rodney Pont | last post: by
2 posts views Thread by Karl Pech | last post: by
5 posts views Thread by Jay | last post: by
1 post views Thread by Roland Rickborn | last post: by
10 posts views Thread by pycraze | last post: by
1 post views Thread by Chunekit Pong | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.