Basics
VBScript
and JScript/JavaScript do not have native functions/commands
to read and write files, because these languages were designed as
"safe" client-side programming languages. Denied access to file system is it's
primary feature. Server-side ASP code and
applications has another work
requirements, file system access (read/write files) is at first of
them. The only way to work with files is to use built-in or external
ActiveX or COM
object. Microsoft solved this problem using
FileSystemObject - but the object cannot read/write binary files (There is some
work-around to use it to store binary files also - see code bellow - but you can
only read text files with this object). Many
people work on this simple task in C++, VBA, Delphi and other languages to
create object, which let's you read binary data. One of this great objects is our
ByteArray class (member of ScriptUtilities
library), which let's you read and write binary files, work with binary
data using blocks, convert it to/from Unicode string using more than 100 code
pages, convert to hex string, etc.
Please see pure-asp file upload with progress bar
if you want to upload and save files from client-side.
This article shows several ways to work with binary
files on local and remote computers (with http/ftp) using free objects from Microsoft.
1. ADODB.Stream object
ADODB.Stream is a first object
you can use to read/write text and binary files. The object is included in ADO
2.5 and later.
a) SaveBinaryData
Function SaveBinaryData(FileName, ByteArray)
Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2
'Create Stream object
Dim BinaryStream
Set BinaryStream = CreateObject("ADODB.Stream")
'Specify stream type - we want To save binary data.
BinaryStream.Type = adTypeBinary
'Open the stream And write binary data To the object
BinaryStream.Open
BinaryStream.Write ByteArray
'Save binary data To disk
BinaryStream.SaveToFile FileName, adSaveCreateOverWrite
End Function
|
b) SaveTextData
ADODB.Stream let's you also save
text data and let's you specify charset (codepage) for text-to-binary data
conversion (against of Scripting.TextStream object).
Function SaveTextData(FileName, Text, CharSet)
Const adTypeText = 2
Const adSaveCreateOverWrite = 2
'Create Stream object
Dim BinaryStream
Set BinaryStream = CreateObject("ADODB.Stream")
'Specify stream type - we want To save text/string data.
BinaryStream.Type = adTypeText
'Specify charset For the source text (unicode) data.
If Len(CharSet) > 0 Then
BinaryStream.CharSet = CharSet
End If
'Open the stream And write binary data To the object
BinaryStream.Open
BinaryStream.WriteText Text
'Save binary data To disk
BinaryStream.SaveToFile FileName, adSaveCreateOverWrite
End Function
|
c) ReadBinaryFile
Function ReadBinaryFile(FileName)
Const adTypeBinary = 1
'Create Stream object
Dim BinaryStream
Set BinaryStream = CreateObject("ADODB.Stream")
'Specify stream type - we want To get binary data.
BinaryStream.Type = adTypeBinary
'Open the stream
BinaryStream.Open
'Load the file data from disk To stream object
BinaryStream.LoadFromFile FileName
'Open the stream And get binary data from the object
ReadBinaryFile = BinaryStream.Read
End Function
|
d) ReadTextFile
Function ReadTextFile(FileName, CharSet)
Const adTypeText = 2
'Create Stream object
Dim BinaryStream
Set BinaryStream = CreateObject("ADODB.Stream")
'Specify stream type - we want To get binary data.
BinaryStream.Type = adTypeText
'Specify charset For the source text (unicode) data.
If Len(CharSet) > 0 Then
BinaryStream.CharSet = CharSet
End If
'Open the stream
BinaryStream.Open
'Load the file data from disk To stream object
BinaryStream.LoadFromFile FileName
'Open the stream And get binary data from the object
ReadTextFile = BinaryStream.ReadText
End Function
|
2. Scripting.FileSystemObject object
You
can also use TextStream object from Scripting library to store binary data. This
function may be useable on servers without ADO installed. This function uses
BinaryToString from Convert a binary data
(BinaryRead) to a string by VBS article.
a) SaveBinaryDataTextStream
Function SaveBinaryDataTextStream(FileName, ByteArray)
'Create FileSystemObject object
Dim FS: Set FS = CreateObject("Scripting.FileSystemObject")
'Create text stream object
Dim TextStream
Set TextStream = FS.CreateTextFile(FileName)
'Convert binary data To text And write them To the file
TextStream.Write BinaryToString(ByteArray)
End Function
|
3. WinHttpRequest (XMLHTTP, ServerXMLHTTP) and remote files.
Microsoft created several
objects, which let's
you read remote binary and text
files. First of them was Microsoft.XMLHTTP - the object is using WinINET API,
so there is no good idea to use it on server-side ASP script. Thanks to WinINET
API, XMLHTTP has a great functionality - it supports any URL - http, https,
ftp and gopher. The URL looks like protocol://usename:password@server/folder/file?parameters.
Other MS HTTP objects are using new WinHTTP
interface. There are three object you can use:
MSXML2.ServerXMLHTTP
WinHttp.WinHttpRequest.5
WinHttp.WinHttpRequest
ServerXMLHTTP and WinHttpRequest objects has a lot of properties and
methods, which let's you specify proxy, additional request headers and so on.
You can download latest versions of WinHttp objects from msdn site, they are also included in some windows service packs and new windows versions.
See also post binary data to URL from WSH/ASP/VBA/VBS.
a) BinaryGetURL
Function BinaryGetURL(URL)
'Create an Http object, use any of the four objects
Dim Http
' Set Http = CreateObject("Microsoft.XMLHTTP")
' Set Http = CreateObject("MSXML2.ServerXMLHTTP")
Set Http = CreateObject("WinHttp.WinHttpRequest.5.1")
' Set Http = CreateObject("WinHttp.WinHttpRequest")
'Send request To URL
Http.Open "GET", URL, False
Http.Send
'Get response data As a string
BinaryGetURL = Http.ResponseBody
End Function
|
You
can use ResponseText property to get the document data as a string (or BinaryToString VBS
function).
|