Home All Groups Group Topic Archive Search About

Streaming a file to text?

Author
15 Jun 2006 9:01 AM
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.

Author
15 Jun 2006 9:09 AM
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.

--
Peter Macej
Helixoft - http://www.vbdocman.com
VBdocman - Automatic generator of technical documentation for VB, VB
..NET and ASP .NET code
Author
15 Jun 2006 11:47 AM
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
Author
15 Jun 2006 9:10 AM
Andrew Morton
Hugh Janus wrote:
> 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
Author
15 Jun 2006 7:09 PM
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

Show quoteHide quote
> 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.
>
Author
16 Jun 2006 5:56 AM
Hugh Janus
GhostInAK wrote:
> 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
Author
16 Jun 2006 9:50 AM
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
Author
19 Jun 2006 7:37 AM
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

Show quoteHide quote
> GhostInAK wrote:
>
>> 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
>