function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AMRITA AGARWALA 2AMRITA AGARWALA 2 

Convert Salesforce Attachment.Body to a file in javascript

Hi,
I have received the Attachement.Body from REST API call as mentioned in https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_sobject_blob_retrieve.htm from client side. 
When I get the blob returned and try to create a js file object,as below: 
    var body = [blobData];
    this.file = new File(body , fileName , {type: contentType , lastModifiedDate: new Date(), name: fileName} );
Now btoa(blobData) throws error Uncaught DOMException: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range  for image/pdf. It works fine for text.
Please help to figure this out. 
pconpcon
You will need to set the body of the attachment to a string using Blob.toString().
AMRITA AGARWALA 2AMRITA AGARWALA 2
this does not give me desired result. 
AMRITA AGARWALA 2AMRITA AGARWALA 2
solution : (unescape(encodeURIComponent(blobData))) ;
vignesh sethuramalingam 17vignesh sethuramalingam 17
Hi Amrita,

I am trying to achieve the same functionality, Can you please help me with sample code.

Thank you in advance

 
pavan.elthepu1.392651269495206E12pavan.elthepu1.392651269495206E12
Can anyone post sample code for this?

I'm getting An invalid XML character (Unicode: 0x8) was found in the element content of the document. error if I'm using (unescape(encodeURIComponent(blobData))) ;​

And blank PDF is generating if I'm using btoa(unescape(encodeURIComponent(data)));

Please help me with this.
DuckyDucky
The solution is:
1. convert attachment.Body into base64 data (in Apex code)
2. convert base64 data into blob data (in Javascript)

I tried with this and it works perfectly. Sample code for this solution is
1. in Apex code:
//declare a variable to pass data to Visualforce Page
public string body{get;set;}


//get the attachment data at anywhere in your class
body = EncodingUtil.base64Encode(attachment.Body);



2. in Javascript:
function b64toBlob(b64Data, contentType, sliceSize) {
  contentType = contentType || '';
  sliceSize = sliceSize || 512;
  var byteCharacters = atob(b64Data);
  var byteArrays = [];
  for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
    var slice = byteCharacters.slice(offset, offset + sliceSize);
    var byteNumbers = new Array(slice.length);
    for (var i = 0; i < slice.length; i++) {
      byteNumbers[i] = slice.charCodeAt(i);
    }
    var byteArray = new Uint8Array(byteNumbers);
    byteArrays.push(byteArray);
  }

  var blob = new Blob(byteArrays, {type: contentType});
  return blob;
}


function SaveDataAs(){
  var blob = b64toBlob("{!body}", "{!attachment.ContentType}");
  var blobUrl = URL.createObjectURL(blob);
  window.location = blobUrl;
}


 
Gaston DovalGaston Doval
Thank you so much Ducky , this worked like a charm.