PC Review


Reply
Thread Tools Rate Thread

Base64 directly to file

 
 
sead.alispahic@gmail.com
Guest
Posts: n/a
 
      18th Jan 2007
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?

 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      18th Jan 2007
<(E-Mail Removed)> wrote:
> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Abubakar
Guest
Posts: n/a
 
      18th Jan 2007
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

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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?
>



 
Reply With Quote
 
=?ISO-8859-2?Q?Marcin_Grz=EAbski?=
Guest
Posts: n/a
 
      18th Jan 2007
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 Removed) 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?
>

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      18th Jan 2007
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

 
Reply With Quote
 
=?ISO-8859-2?Q?Marcin_Grz=EAbski?=
Guest
Posts: n/a
 
      18th Jan 2007
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
 
Reply With Quote
 
sead.alispahic@gmail.com
Guest
Posts: n/a
 
      18th Jan 2007
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
>
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >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?
> >


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert Excel file to base64 Arvind P Rangan Microsoft VB .NET 1 3rd May 2006 01:49 PM
Outlook 2003 and Base64 file attachments Mark Microsoft Outlook Discussion 1 2nd May 2006 01:04 PM
Posting a Base64 encoded file to a web server Kevin Microsoft VB .NET 1 20th Oct 2005 11:43 AM
sending email with base64 encoded file =?Utf-8?B?RGFtaWVuIERlYmlu?= Microsoft Dot NET Framework 0 11th Jan 2005 07:21 PM
Convert base64 string to file Kliot Microsoft VB .NET 4 6th May 2004 04:45 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:11 AM.