Using 'use_blobs'

When opening a file with ‘use_blobs’ set (as documented here Help Portal), I am not clear what object is returned now how to process contents using it. The normal Node Buffer/Blob methods throw an internal error, and simple iteration doesn’t work either.

Here is an example of something I have tried

  main: function(record_id, parameters, rule) {
    const file = cs.read_file(record_id, params.custom_paths.file);

    cs.log(\[file\]); // \[binary data, length 87071299B\]

	const stream = file.stream(); // An internal error occurred

    let i = 0;

    for (const c of file) { // TypeError: Result of the Symbol.iterator method is not an object

        cs.log(c);          // this line is not reached

        if (i++ > 50) break;

    }

}

Hi,

If I remember correctly you can call toString() on the file object it should return you the file content.

Hey

“blobs” are basically a wrapper around strings, as Javascript strings are not suitable for binary data as they’re UTF16

But you treat blobs like strings - you can append them, concatenate them etc for the most part just as if they were regular strings without their encoding being converted. Many of the common JS String functions work.

You just have to be aware though that if you do use toString() to get at its contents for anything other than perhaps debugging, it’ll be UTF16 encoded and likely give you problems if you try and write the file back and it’s supposed to be a binary format (e.g. image, msg, pdf, etc).

If (based on your code) you’re just trying to log the first 50 characters of it, you can probably just do:

cs.log(file.substr(0, 50))

Hope that helps

Mark

1 Like