Skip to content

Loading and saving data

Ingvar Stepanyan edited this page Sep 2, 2013 · 7 revisions

In most cases you don't have binary data hard-coded in your JavaScript (why do you need any operation on it otherwise, eh?).

So you need to get this data from some external source and show result when you are done.

And jBinary provides handy methods for that:

  • jBinary.loadData(source, callback) (static method):

    Loads data from given source and returns it in Node.js-like callback(error, data).

    Source can be one of (if supported on current engine):

  • jBinary.load(source, [typeSet,] callback) (static method):

Loads data from given source using jBinary.loadData, detects typeset using Repo associations if it's not specified explicitly, creates jBinary instance on this data and typeset and returns it in callback(error, binary).

If Repo is not used, jBinary.load would accept only explicit typeset objects (no loading by name nor file format auto-detection).

  • toURI(mimeType = 'application/octet-stream')

Returns URI suitable for usage in DOM elements (uses Blob URIs where supported, data-URIs in other cases, so may be problematic when creating from big data in old browsers).

Example

fileInput.addEventListener('change', function () {
  jBinary.loadData(fileInput.files[0], function (error, data) {
    if (error) {
      return console.log(error);
    }

    // here you get data from <input type="file" /> that you can use in jDataView/jBinary constructors
  });
});

jBinary.load('sample.tar', function (error, binary) {
  if (error) {
    return console.log(error);
  }

  // here TAR format is auto-detected and used by `binary` (in the case you use it in combination with jBinary.Repo; if not, jBinary with default typeset will be used)
  var tar = binary.read('File');

  // ... more code ...
});
Clone this wiki locally