Reading and Writing to Files on the Intranet - Reading and writing binary files (Page 5 of 5 )
Reading and writing binary data is almost identical to reading and writing text except that the read() and write() methods have to be used. Since binary is one whole file, there are no line separators as in text. The Read() and write() methods accept byte values and return byte values.
Reading a BMP file and persisting it to another location
This example uses the stream object to read a bitmap file on one of the folders and saves it to another folder on the intranet using the SaveToFile() method. The red.bmp file represents a 16 x 16 square filled with red color and takes up 821 bytes.
Private Sub Command1_Click()
Dim strm As ADODB.stream
Set strm = New ADODB.stream
'Stream type is binary
strm.Type = adTypeBinary
'open the file red.bmp
strm.Open "Url=http://localhost/DevHome/DevArticles/red.bmp"
'save the file to root directory of ASPFree
strm.SaveToFile "C:InetpubwwwrootDevHomeASPFreeTest.bmp", _
adSaveCreateOverWrite
strm.Close
End Sub
When the Test.bmp is browsed you will see the saved bmp file as shown.

Reading a binary file and placing it in a buffer
The same red.bmp file can be read to a buffer as follows. The bytes read into the buffer are in the bufread variable. The upper bound of this shows the size of the red.bmp file read, which is 821 bytes. The read also adds an extra byte at the end position.
Private Sub Command2_Click()
Dim bufread() As Byte
Dim strm As ADODB.stream
Set strm = New ADODB.stream
strm.Type = adTypeBinary
strm.Open "URL=http://hodentek/DevHome/DevArticles/red.bmp"
bufread = strm.Read(ADODB.StreamReadEnum.adReadAll)
MsgBox (UBound(bufread))
Dim mycount As Long
For mycount = 0 To UBound(bufread)
Debug.Print mycount & "," & (bufread(mycount))
Next mycount
strm.Close
End Sub
Writing a binary file
The data that gets into the buffer will be a byte array and the Stream's write method will write to this array as shown in the listing.
Option Compare Database
Private Sub Command0_Click()
Dim mybuf(3) As Byte
Dim rec As ADODB.Record
Dim strm As ADODB.stream
Set rec = New ADODB.Record
Set strm = New ADODB.stream
rec.Open "jay.dat", "URL=http://hodentek/DevHome/DevShed/", _
adModeReadWrite, adCreateNonCollection + adCreateOverwrite
strm.Type = adTypeBinary
strm.Open rec, adModeReadWrite, adOpenStreamFromRecord
mybuf(0) = 100
mybuf(1) = 25
mybuf(2) = 210
strm.Write (mybuf)
'jay.dat file with 4 Bytes will be created
'with the 4th byte, the end of file
strm.Close
rec.Close
End Sub
Summary
The tutorial describes with examples writing and reading from files on the intranet. The process of reading and writing is much cleaner than using the earlier versions of ADO (2.0, 2.1). Since the Stream object can be opened without any arguments, the stream will be in memory and can be persisted to a file on the hard disk. This is a risky procedure as any program which gives access to writing files to the hard disk should be avoided, or used with caution. The IE browser has inherent support for ADODB.Stream objects, and as vulnerabilities in IE can be exploited, it needs to be disabled. Follow this link for a recipe to disable the support and use caution when saving files to hard disk.
Using the SaveToFile and LoadFile features (which were not discussed, but easy to implement) makes transferring binary files very easy. The argument for the ReadText() which reads everything has to be looked up in a reference or guessed, since intellisense gives no help. While reading text files, unless the charset is specified as ASCII, setting stream.type alone will not suffice.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |