Streaming a file to text?

H

Hugh Janus

Hi all.

Is it possible to take a binary file and out put it as text so that I
can store it in a text field of a database? And then later, take the
text and 're-stream' it as the original binary file. If so, any ideas
on where I could start?

I cannot store the file in any other format other than text so I can
only do a converstion to text and then back from it.

Thanks in advance.
 
P

Peter Macej

Is it possible to take a binary file and out put it as text so that I
can store it in a text field of a database? And then later, take the

You can use Convert class and its ToBase64String and FromBase64String
methods for such tasks.
 
A

Andrew Morton

Hugh said:
Is it possible to take a binary file and out put it as text so that I
can store it in a text field of a database? And then later, take the
text and 're-stream' it as the original binary file. If so, any ideas
on where I could start?

I cannot store the file in any other format other than text so I can
only do a converstion to text and then back from it.

I think you want to look at System.Convert.ToBase64

Andrew
 
H

Hugh Janus

You can use Convert class and its ToBase64String and FromBase64String
methods for such tasks.

Thanks for the quick reply. Do you (or anyone else) have any sample
code I can start with by any chance?

Hugh
 
G

GhostInAK

Hello Hugh,

In general this is a TERRIBLE idea. You should try to avoid storing files
in a database if at all possible. Instead, think about storing just the
file path.

-Boo
 
H

Hugh Janus

GhostInAK said:
Hello Hugh,

In general this is a TERRIBLE idea. You should try to avoid storing files
in a database if at all possible. Instead, think about storing just the
file path.

-Boo

I totally agree, but it seems the only option I have and thats how my
boss wants it. Thankfully the files are all quite small so i don't
envisage too many probs DB wise. I just need a kick start with some
code!

Hugh
 
H

Hugh Janus

OK, I think I might be getting somewhere. I think I can convert the
file to text OK however, upon conversion back to a file it gives the
error "Longitud no válida para una cadena Base-64" which roughly
translates to "Length not valid for a Base-64 chain". Below is the
code I am using.

Any help would be greatly appreciated. My problem could be that the
decode is working fine but the encoding to Base64 is failing
perhaps??!!?!


Public Sub FileToText(ByVal FileName As String, ByVal
OutputFileName As String)

Dim TextWriter As New IO.StreamWriter(OutputFileName)
Dim NumBytesRead As Integer
Dim BytesToConvert(1000) As Byte
Dim Limit As Integer

Dim fs As New IO.FileStream(FileName, IO.FileMode.Open)

Do Until fs.Position = fs.Length

' Dim binaryData() As Byte =
Convert.ToBase64String(fs.Read)

NumBytesRead = fs.Read(BytesToConvert, 0,
BytesToConvert.Length)

TextWriter.Write(Convert.ToBase64String(BytesToConvert)) ',
0, binaryData.Length)

Loop

fs.Close()
TextWriter.Close()

End Sub

Public Sub FileFromText(ByVal FileName As String, ByVal
OutputFileName As String)

Dim TextReader As New IO.StreamReader(FileName)
Dim strRead As String

Dim fs As New IO.FileStream(OutputFileName, IO.FileMode.Create)

Do

strRead = TextReader.Read.ToString

If Not strRead Is Nothing Then

Dim binaryData() As Byte =
Convert.FromBase64String(strRead)

fs.Write(binaryData, 0, binaryData.Length)

End If

Loop

fs.Close()
TextReader.Close()

End Sub
 
G

GhostInAK

Hello Hugh,

Is there some reaosn you want to convert it to text? Most databi have a
method for storing binary data natively. I'm speaking off the top of my
head without lookin up the doco.. but SQL Server I know has a BINARY field
type and I believe it also has a VARBINARY (possibly).

-Boo
 

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