Base64 directly to file

S

sead.alispahic

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?
 
J

Jon Skeet [C# MVP]

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.
 
A

Abubakar

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
 
?

=?ISO-8859-2?Q?Marcin_Grz=EAbski?=

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

(e-mail address removed) napisa³(a):
 
J

Jon Skeet [C# MVP]

Marcin said:
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
 
?

=?ISO-8859-2?Q?Marcin_Grz=EAbski?=

Hi Jon,

Jon Skeet [C# MVP] napisa³(a):
Marcin said:
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
 
S

sead.alispahic

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;
}
//--------------------------------------------------------------------------------
 

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

Top