Jsforce: Retreiving blob values as text

Created on 16 Jun 2015  路  7Comments  路  Source: jsforce/jsforce

Hi, the new 'Notes' beta saves all content as a blob filetype and while I understand how to output a blob to a file I am wondering if there is a way to convert the blob to text? I am conscious this may not be good practice but before I revert back to the old notes I thought i'd check first. It's not possible with Remote Objects either just in case...

I have the content URI which is in the format;

/services/data/v33.0/sobjects/ContentNote/{id}/Content

question

All 7 comments

You can convert the stream to string.

var contentStream = conn.sobject('ContentNote').record(id).blob('Content');
var buf = [];
contentStream.on('data', function(data, enc) {
  buf.push(data.toString(enc));
});
contentStream.on('end', function() {
  var contentStr = buf.join('');
  // handle contentStr
});

ah, here is the correct way to get the blob! :)

Does this approach work in the browser? I'm able to access the blob, and I can see the blob data being returned in a response object, but my event handlers are not being called, and it's not clear how to access the response object. Any help would be greatly appreciated.

Get around my issue by using the Connection.request() method instead of blob().

If you want to get data from the Attachment-object, you could do this almost the same way, but you should use Buffer.concat(buf), instead of buf.join('') and skip the toString-decoding.

I'm facing the same issue as @pordonez. Would you mind elaborating how you solve it? I haven't found any documentation about it and I think @stomita snippet is outdated.

Connection.request gave me a headache when retrieving images. The response was always a garbled Unicode string. The Fetch API gave me no such issues.

// response.Body will store the API route to the image
const response = await conn
  .sobject('Attachment')
  .retrieve(id)
  .catch(handleConnectionError);

// we use fetch instead of jsforce.Connection.retrieve because jsforce
// can return a garbled string when downloading images
const fetchResponse = await fetch(`${conn.instanceUrl}/${response.Body}`, {
  headers: { Authorization: `Bearer ${conn.accessToken}` },
});
const blob = await fetchResponse.blob();

// return the blob instead if you don't need an src url
return URL.createObjectURL(blob);
Was this page helpful?
0 / 5 - 0 ratings

Related issues

JonDum picture JonDum  路  7Comments

vivinRajagopalan picture vivinRajagopalan  路  6Comments

MoshikEilon picture MoshikEilon  路  3Comments

MGarfOppLoans picture MGarfOppLoans  路  7Comments

myke11j picture myke11j  路  4Comments