• <GetStart>
  • CSPro User's Guide
    • The CSPro System
    • Data Dictionary Module
    • The CSPro Language
    • Data Entry Module
    • Batch Editing Applications
    • Tabulation Applications
    • Data Sources
    • CSPro Statements and Functions
    • Templated Reporting System
    • HTML and JavaScript Integration
    • Action Invoker
      • Overview
      • Execution Environments
      • Security and Formatting Options
      • Base Actions
      • Application Namespace
      • Clipboard Namespace
      • Data Namespace
      • Dictionary Namespace
      • File Namespace
        • File Action Invoker Namespace
        • File.copy Action
        • File.readBytes Action
        • File.readLines Action
        • File.readText Action
        • File.writeBytes Action
        • File.writeLines Action
        • File.writeText Action
      • Hash Namespace
      • Localhost Namespace
      • Logic Namespace
      • Message Namespace
      • Path Namespace
      • Settings Namespace
      • Sqlite Namespace
      • System Namespace
      • UI Namespace
    • Appendix
  • <CSEntry>
  • <CSBatch>
  • <CSTab>
  • <DataViewer>
  • <TextView>
  • <TblView>
  • <CSFreq>
  • <CSDeploy>
  • <CSPack>
  • <CSDiff>
  • <CSConcat>
  • <Excel2CSPro>
  • <CSExport>
  • <CSIndex>
  • <CSReFmt>
  • <CSSort>
  • <ParadataConcat>
  • <ParadataViewer>
  • <CSCode>
  • <CSDocument>
  • <CSView>
  • <CSWeb>

File.readBytes Action

Format
s = CS.File.readBytes(path := ...ʃ, bytesFormat := ...ʅ)
ArgumentDescriptionTypes / Required
pathThe path of the file to read.string
required
bytesFormatThe format in which the bytes are returned.
The default value is "dataUrl".
string
not required
Description
The File.readBytes action reads a file, specified as path, in binary format, returning the entire file's content as bytes. The argument bytesFormat allows you to specify the format of the returned binary data.
Return Value
The action returns the file's content in bytes, represented as a string in one of the output formats used for binary data.
Exceptions
The action throws an exception if any of its arguments are not specified in a valid form or if the file does not exist or cannot be read.
Example (JavaScript)
// read all the bytes from a file
CS.File.readBytesAsync({
    path: 
"legacy.amo",
    bytesFormat: 
"hex"
})
.then(hexBytes => {
   
// remove every other byte, represented in hexadecimal format (two characters per byte), from the file
    let removeNextByte = false;

   
for( let i = 0; i < hexBytes.length; ) {
       
if( removeNextByte ) {
            hexBytes = hexBytes.substring(0, i) + hexBytes.substr(i + 2);
            removeNextByte = 
false;
        }
       
else {
            removeNextByte = 
true;
            i += 2;
        }
    }

   
// write the bytes
    return CS.File.writeBytesAsync({
        path: 
"legacy (every-other-byte).amo",
        bytes: hexBytes,
        bytesFormat: 
"hex"
    });
})
.then(() => console.log(
"Successfully read, removed half the content, and wrote the bytes."))
.
catch(error => console.log(error));
See also: File Action Invoker Namespace, File.writeBytes Action, File.readBytes Action, File.readLines Action