'Convert the binary file to base64 encoding
Public Function convertfiletobase64 (byval filename) as string
Dim file_length as long
Dim fnum as integer
Dim bytes () as byte
Dim TXT as string
Dim I as integer
File_length = filelen (filename)
Fnum = freefile
Redim bytes (1 to file_length)
Open filename for binary as # fnum
Get # fnum, 1, bytes
Close fnum
TMP = encodebase64 (bytes)
Convertfiletobase64 = TMP
End Function
private function encodebase64 (byref arrdata () as byte) as string
dim objxml as msxml2.domdocument
dim objnode as msxml2.ixmldomelement
'help from MSXML
set objxml = new msxml2.domdocument
'byte array to base64
set objnode = objxml. createelement ("b64")
objnode. datatype = "bin. base64 "
objnode. nodetypedvalue = arrdata
encodebase64 = objnode. text
'thanks, bye
set objnode = nothing
set objxml = nothing
end function
private function decodebase64 (byval strdata as string) as byte ()
dim objxml as msxml2.domdocument
dim objnode as msxml2.ixmldomelement
'help from MSXML
set objxml = new msxml2.domdocument
set objnode = objxml. createelement ("b64")
objnode. datatype = "bin. base64 "
objnode. TEXT = strdata
decodebase64 = objnode. nodetypedvalue
'thanks, bye
set objnode = nothing
set objxml = nothing
end function
'Restore a base64 encoded file to a binary file
Public Function convertfilefrombase64 (byval fromname as string, byval toname as string)
dim textline
open fromname for input as #1 'open the file.
do while not EOF (1) 'loops to the end of the file.
line Input #1, textline' reads a row of data and assigns it to a variable.
loop
close #1 'close the file.
dim A () as byte
'redim A (1 to Len (textline)
A = decodebase64 (textline)
fnum = freefile
open toname for binary as # fnum
put # fnum, 1, A
close fnum
end function
'Read a text file
Public Function readtextfile (byval filename as string) as string
Dim textline
Open filename for input as #1 'open the file.
Do while not EOF (1) 'loops to the end of the file.
Line input #1, textline' reads a row of data and assigns it to a variable.
Loop
Close #1 'close the file.
Readtextfile = textline
End Function
'Write a text file
Public Function writetextfile (byval filename as string, byval strtowrite as string)
Open filename for output as # fnum
Print # fnum, strtowrite
Close # fnum
End Function