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, http,
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).
See also
for 'Work with binary files in VBSscript - read and write local and remote files' article
Download multiple files in one http requestThis article shows a way to download multiple files in one http request. It let's you send an HTML page along with image preview, prepare more files for download and send the files as one data stream. One request, one authentication per multiple files.
Read and write SQL image data, store binary file to sql table.Store and read SQL image/binary data using functions in this article. You can store local or remote files in an sql table along with a description and other fields.
Post binary data to URL from WSH/ASP/VBA/VBScript.Two methods to POST binary or string data to external URL and read results sent from a script on the URL.
Convert a binary data (BinaryRead) to a string by VBSThis article, demonstrates several versions of source VBS code you can use to work with binary data in ASP and convert the data to a String format.
Create and work with binary data in ASP/VBScriptLets you convert/create binary data in ASP to use the data for BinaryWrite/BinaryRead.
Write to text file from MS-SQL.Stored procedure which enables write text data directly to the file.
Copyright and use this code
The source code on this page and other samples at
http://www.motobit.com/tips/
are a free code, you can use it as you want: copy it, modify it, use it in your products, ...
If you use this code, please:
1. Leave the author note in the source.
or
2. Link this sample from you page.
<A
Href="http://www.motobit.com/tips/detpg_read-write-binary-files/"
Title="Reading and writting binary and text
files is a first task
you will need to solve
in server-side ASP. This article
contains several VBS functions which
let's you store data to
local disk and read local
or remote (http) files."
>Work with binary files in VBSscript - read and write local and remote files</A>