portal.getMultipartText() returns null

Enonic version: 6.6.2-SNAPSHOT
OS: Mac

This works:

    const s1 = getMultipartStream('attachments-1');

    const lines = readLines(s1);
    log.info('Num lines: %s', lines.length);
    log.info('Line 1:%s', stringify(lines[0]));

    const text = readText(s1);
    log.info('Num chars: %s', text.length);
    log.info('Text: %s', stringify(text));

This does not work:

    const t1 = getMultipartText('attachments-1');  // returns null
    log.info('Text length:%s', stringify(t1.length)); // fails here with TypeError: Cannot get property "length" of null
    log.info('Text:%s', stringify(t1));

The getMultipartText() function returns null if the part is not of a text content type ("text/*" or "application/json").

How do you post the data?
Try printing out the form with getMultipartForm() or getMultipartItem() to check its content type.

Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryfMtjQ1TPCPbTsA06

The attachment itself was a png image.

If you print out the multipart received:

var portalLib = require('/lib/xp/portal');
var multipartForm = portalLib.getMultipartForm();
log.info('Multipart %s', multipartForm);

you will get something like this:

    "file1": {
        "name": "file1",
        "fileName": "image.png",
        "contentType": "image/png",
        "size": 858.0

But in any case, it does not make so much sense to do getMultipartText() for an image.
If what you need is the size you can get it with portalLib.getMultipartItem('attachments-1').size

No I need the raw binarydata, base64 encode it and post it on another rest api using json :slight_smile:

Ah ok :open_mouth: . I’m not sure if that can be easily done by handling strings in JavaScript.
A JavaScript character does not correspond with a byte, a JS char is UTF-16.

I would maybe make a stream-to-base64 lib function in Java.

It would be very nice if Enonic could give us a stream-to-base64 function in some lib, portal?

It could also be nice with a list of recommended node modules…

I tried: https://www.npmjs.com/package/base-64 but it seems only handles latin1 text.

Then I tried: https://www.npmjs.com/package/js-base64

But it requires the global object to be passed in.

It seems like this one https://www.npmjs.com/package/base64-js handles binary data, but I’m doing something wrong as the png image did not appear on the screen in the third party website.

Perhaps I could have used this https://www.npmjs.com/package/base64-stream directly on the stream.
But it looks like to many node dependancies.

The fromByteArray in base64-js takes uint8. So I may have to do something like this: http://stackoverflow.com/questions/6965107/converting-between-strings-and-arraybuffers

With node this would have been this easy:


But there is no Buffer API without node.

Perhaps I can use this https://www.npmjs.com/package/buffer it’s made for the browsers though.

Ach what rabbit hole have I gotten myself into for such a little simple standard thing to do.

I think I will need some help from Enonic on this one…