473,397 Members | 2,077 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,397 software developers and data experts.

Streaming a file to text?

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.

Jun 15 '06 #1
7 1353
> 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
Jun 15 '06 #2
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

Jun 15 '06 #3
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

Jun 15 '06 #4
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
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.

Jun 15 '06 #5

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

Jun 16 '06 #6
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(BytesToCon vert)) ',
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

Jun 16 '06 #7
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
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

Jun 19 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Markus Ernst | last post by:
Hi I know this question is rather HTTP related than PHP, but I did not find an HTTP group on my news server. I deliver some files with the following PHP syntax: header('Content-Description:...
22
by: googlegroups | last post by:
I am playing with the XMLHTTPRequest method to perform client/server transactions. I have it set up right now so that when readyState is 4, it takes the XML and processes it. This works great until...
5
by: Eric Sabine | last post by:
I have a web site where the user is able to download a file. I can't use an anchor tag because I don't want the user to see where the filename is coming from. Below is the code in which the user...
3
by: A.M-SG | last post by:
Hi, I have a ASP.NET aspx file that needs to pass large images from a network storage to client browser. The requirement is that users cannot have access to the network share. The aspx file...
6
by: | last post by:
Hi all, is there a better way to stream binary data stored in a table in sql 2005 to a browser in .net 2.0? Or is the code same as in .net 1.1? We noticed that in certain heavy load scenarios,...
1
by: DB | last post by:
Hi All, I’m trying to stream a PDF with .NET 2.0 in a c# web app. However, it does not actually show the PDF (using adobe acrobat). The Situation: Click on a Button, Opens a new Window,...
1
by: Volte | last post by:
Hey all. Been looking into this for a while now, got some mock ups and whatnot. All works well (as far as the server "streaming" the data, via a sleep and flush method right now just for...
8
by: Amjad | last post by:
Hi i am writing a application where i want to browse video file and copy data into stream and send that stream over network...I have develop P2P windows application where i successfully transfer...
2
by: SPG | last post by:
Hi, Two questions for you all.. Firstly, is there a way of streaming video using PHP? At the moment I just have a link to a video file and the whole thing downloads before playing which is a...
1
by: Faisal Shafiq | last post by:
I want to upload a file direct to the Silverlight Streaming Service from a Web Client such as silverlight application. As per our product requirement we want to upload a .WMV file directly from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.