|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
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. > Is it possible to take a binary file and out put it as text so that I You can use Convert class and its ToBase64String and FromBase64String > can store it in a text field of a database? And then later, take the 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 > You can use Convert class and its ToBase64String and FromBase64String Thanks for the quick reply. Do you (or anyone else) have any sample> methods for such tasks. > code I can start with by any chance? Hugh Hugh Janus wrote:
> Is it possible to take a binary file and out put it as text so that I I think you want to look at System.Convert.ToBase64> 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. Andrew 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. > GhostInAK wrote:
> Hello Hugh, I totally agree, but it seems the only option I have and thats how my> > 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 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 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 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 >
changing the date format in vb.net
Datasets - use 1 or many? How to force upper case in a DataGridView column? Calling a batch file from vb.net with parameters Dealing with NULL values in Integer fields Mulples threads and impersonation What's the Best Startup Scenario for Unattended Server Execution? VB.net 2005 splash screen Handle pointers from delphi client to a vb.net dll Referencing Function Arguments |
|||||||||||||||||||||||