473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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 27129
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Rodney Pont | last post by:
I've got the example below to set up phpOpenTracker to log exit URL's but I'm having trouble getting it to work. I have played with the quotes and changed the \\2 to $3 and got the url in there but...
2
by: Karl Pech | last post by:
Hi all, I'm trying to write a program which can read in files in the following format: sos_encoded.txt: --- begin-base64 644 sos.txt UGxlYXNlLCBoZWxwIG1lIQ== ---
27
by: gRizwan | last post by:
Hello all, We have a problem on a webpage. That page is sent some email data in base64 format. what we need to do is, decode the base64 data back to original shape and extract attached image...
7
by: Neo Geshel | last post by:
Greetings. I have managed to stitch together an awesome method of posting text along with an image to a database, in a way that allows an unlimited number of previews to ensure that text and...
5
by: Jay | last post by:
I have bean trying to get my head around reading .GIF files from base64 strings, Basically I need to specify a filename and convert it to base64 then I can copy/past the string to wear I want it....
4
by: Russell Warren | last post by:
I've got a case where I want to convert binary blocks of data (various ctypes objects) to base64 strings. The conversion calls in the base64 module expect strings as input, so right now I'm...
1
by: Roland Rickborn | last post by:
Hallo zusammen, in meine Anwendung ist ein Bild eingebettet und oben in der Leiste soll ein Icon erscheinen. Ausserdem will ich nur _eine_ Datei ausgeben, also ohne zusärtliche Bild-Dateien...
10
by: pycraze | last post by:
Hi , I am currently trying to implement base64 encoding and decoding scheme in C . Python has a module , base64 , that will do the encoding and decoding with ease . I am aware of OpenSSL having...
1
by: Chunekit Pong | last post by:
How to save a Base64 encoded string to a binary file For instance, the following XML tag stores the binary data in a Base64 encoded string <data>TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx<data> ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.