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));
aro
August 30, 2016, 4:01pm
2
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.
aro
August 31, 2016, 9:02am
5
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
aro
August 31, 2016, 12:17pm
7
Ah ok . 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.
opened 03:13PM - 09 Feb 16 UTC
closed 03:15PM - 11 Sep 17 UTC
Please, don't pass global object as argument here https://github.com/dankogai/js-base64/blob/master/base64.js#L11
Build with Babel fails here with error
Requiring external module babel-core/register
/Users/meu/web/node_modules/js-base64/base64.js:16
var _Base64 =...
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.
With node this would have been this easy:
https://github.com/alzubaidi/nodetools/blob/master/lib/tools.js#L12
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…