SaveAs Method of ByteArray class - Write binary data to a file

Method | Member of  ScriptUtils.ByteArray | Changes | Purchase | Download

Description

Saves the ByteArray data as a file.

Syntax

ByteArray.SaveAs (FileName as String, [StartPosition as Variant = 1], [Length as Long = All data])
 

Where Type Optional Default Description
 FileName  String     File to save data. 
 StartPosition  Variant  yes  1  Start file position to save SaveAs contents, based to 1. 0 means 'End of file', Empty or undefined means 'overwrite file'. 
 Length  Long  yes  All data  Number of bytes to write to the file. (1B - 2GB-1) 

Remarks

      Maximum ByteArray length is 2GB, but StartPosition can be 64 bit number (currency or decimal data type), so you can also save data to a file with unlimitted size.

Examples

ByteArray - Save the posted data.

<%
  Set Binary = CreateObject("ScriptUtils.ByteArray")
  Length = CLng(Request.ServerVariables("HTTP_Content_Length"))
  Binary.ByteArray = Request.BinaryRead(Length)
  Binary.SaveAs Server.MapPath(".") & "\data.post"

  Response.Write "Request data (" & Binary.Length & " bytes): <br><pre>"
  Response.Write Binary.String
  Response.Write "</pre>"
%>
<form method=post>
<input value=XXXXXXXXXXX NAME=A>
<input type=submit>
</form>

ByteArray - save unicode data (string) as utf-8 with BOM

See also Batch file conversion - character set and BOM detection of html files to detect files with BOM (unicode Little/Big, utf-8)
'Save unicode string as UTF-8 with BOM

SaveBOMUTF "f:\222.txt", "ìšèøžýáíé"

Sub SaveBOMUTF(FileName, SomeString)
  '1. save BOM header.
  SaveBOMHeader FileName

    Dim ByteArray
  Set ByteArray = CreateObject("ScriptUtils.ByteArray")

  'Convert the string to UTF-8
  ByteArray.CharSet = "utf-8"
  ByteArray.String = SomeString

  'Save the UTF-8 string at position 4 of the file
  '(after the 3bytes BOM header)
  ByteArray.SaveAs FileName, 4
End Sub

Sub SaveBOMHeader(FileName)
  'create byte array object
  Dim ByteArray
  Set ByteArray = CreateObject("ScriptUtils.ByteArray")

  'the bytearray contains BOM header - 3 bytes.
  ByteArray.SetSize 3
  ByteArray(1) = &HEF
  ByteArray(2) = &HBB
  ByteArray(3) = &HBF
  'Or you can use ByteArray.HexString = "EFBBBF" in v> 2.14

  'Save the BOM header to the FileName
  ByteArray.SaveAs FileName
End Sub

ByteArray - accept very large data in POST/PUT request, ASP, VBScript

      You can use Post binary data to URL from WSH/ASP/VBA/VBS and Work with binary files in VBS - read and write local and remote files articles to read and send a binary file from a client-side.
<%
'This sample demonstrates store of large binary data.
'It accepts up to 2GB of a source data a PUT or POST 


SaveLargePostData "C:\temp\uploaddata.dat"

Sub SaveLargePostData(DestFile)
 Const BlockSize = 4096

 Dim TotalBytes, DataReaded, ReadBlockSize, BinaryData

 TotalBytes = Request.TotalBytes
 If TotalBytes > 0 Then
  Dim ByteArray: Set ByteArray = GetByteArray()
 
  For DataReaded = 0 To TotalBytes Step BlockSize
	
	If BlockSize + DataReaded > TotalBytes Then
	'Last block, IIS6 does not accept to read more data
		ReadBlockSize = TotalBytes - DataReaded
	Else
		ReadBlockSize = BlockSize 
	End If
	
	ByteArray.ByteArray = Request.BinaryRead(ReadBlockSize )

	'Save the block of a source data.
	If DataReaded = 0 Then
		'The first block will overwrite existing file (if one)
		ByteArray.SaveAs DestFile
	Else
		'Position of SaveAs (second parameter) is based to 1
		ByteArray.SaveAs DestFile, 1 + DataReaded
	End If
  Next ' DataReaded 
  Response.Write "<br>Source data (" & TotalBytes & _ 
   "B) was saved to '" & DestFile &  "'"
 End If ' TotalBytes>0 then
End Sub





'Returns ByteArray object. Solves problem with registration and installation
Function GetByteArray()
  On Error Resume Next
  Dim Binary
  Set Binary = CreateObject("ScriptUtils.ByteArray") 'Creates ByteArray object
  'response.write hex(Err)
  If Err = &h46 Then
    On Error Goto 0
    Err.Raise 5, "ScriptUtils.ByteArray", "Insufficient permissions to the scptult.ocx file. User: '" & GetUserName & "' must have read permission to the file."
  ElseIf Err = &h1ad Then
    On Error Goto 0
    Err.Raise 6, "ScriptUtils.ByteArray", "Script Utilities is not correctly installed. Please, install Script Utilities on this server or copy and register scptutl.ocx file on this server."
  ElseIf Err = &h8007045A Then
    On Error Goto 0
    Err.Raise 6, "ScriptUtils.ByteArray", "The evaluation version of Script Utilities was expired. Please install full version."
  ElseIf Err = &h8007007E Then
    On Error Goto 0
    Err.Raise 6, "ScriptUtils.ByteArray", "The ScriptUtilities library (scptutl.ocx) is missing. Please copy the library or reinstall the software."
  ElseIf Err <> 0 Then
    Dim E, N
    N = Hex(Err)
    E = Err.Description
    On Error Goto 0
    Err.Raise 6, "ScriptUtils.ByteArray", "Cannot create ScriptUtils.ByteArray object, Error: '" & N & " " & E & "'"
  End If
  Set GetByteArray = Binary
End Function

%>

See also

ByteArray.ReadFrom

Other links for SaveAs

Methods of ByteArray class

Find, FindRev, CharSetConvert, Left, Mid, ReadFrom, Right, SaveAs, SetSize

Properties of ByteArray class

Base64, ByteArray, CodePage, HexString, CharSet, Length, Punycode, String

ScriptUtils.ByteArray

Works with safearray binary data - save/restore binary data from/to a disk, convert to a string/hexstring, codepage/charset conversions, Base64 conversion, etc.
     ByteArray is a COM class specially designed to work with Microsoft Windows Scripting engines - VB Script and JScript in Active Server Pages or WSH and in CHM or HTA applications. It also works with VB Net, Visual basic (VBA - VB 5, VB 6, Word, Excel, Access, …), C#, J#, C++, ASP, ASP.Net, Delphi and with T-SQL OLE functions - see Use ByteArray object article. You can also use the object in other programming environments with COM support, such is PowerBuilder.
     Source code for ByteArray is available within distribution license, please see License page for ASP file upload and ScriptUtilities.

ScriptUtils

Huge ASP upload is easy to use, hi-performance ASP file upload component with progress bar indicator. This component lets you upload multiple files with size up to 4GB to a disk or a database along with another form fields. Huge ASP file upload is a most featured upload component on a market with competitive price and a great performance . The software has also a free version of asp upload with progress, called Pure asp upload , written in plain VBS, without components (so you do not need to install anything on server). This installation package contains also ScriptUtilities library. Script Utilities lets you create hi-performance log files , works with binary data , you can download multiple files with zip/arj compression, work with INI files and much more with the ASP utility.

© 1996 - 2011 Antonin Foller, Motobit Software | About, Contacts | e-mail: info@pstruh.cz


Other Motobit links:   IISTracer, real-time IIS monitor   ASP file upload - upload files to ASP. 
ActiveX/VBSScript registry editor  ActiveX NT User account manager  Export MDB/DBF from ASP Active LogFile  Email export  ActiveX/ASP Scripting Dictionary object