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
Vineeth K MVineeth K M 

Convert the response from API to a document

Hello everyone, in my project am getting a response from an API in application/octet-stream format and i need to convert it to a document (maybe pdf/docx/png etc)  using LWC. And i need to download it. Can someone help me over here?
VinayVinay (Salesforce Developers) 
Hi Vineeth,

What is the api which is used in getting response? You would need to use Content (Blob) to convert to desired format.

https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_blob.htm

Thanks,
zia awanzia awan
I have Convert the response from API to a document.See more detail here : https://atozanimalszoo.com/
Vineeth K MVineeth K M
Hello Vinay, am using rest api. Can you please send me some piece of code to convert the response?
Morgan GoughMorgan Gough
let data = await fetch('api-url'); // fetch data from API
let blob = await data.blob(); // convert it to a Blob
let url = window.URL.createObjectURL(blob); // create a URL for the Blob
let link = document.createElement('a'); // create a link element
link.href = url; // set the href of the link to the Blob URL
link.download = 'filename.extension'; // set the filename and extension
link.click(); // simulate a click to download the file

Please replace 'api-url' with your actual API URL, and 'filename.extension' with your preferred filename and file extension.
This code initiates a download of the file. Please note that it's important to understand the type of file you're dealing with (pdf, docx, png etc.) in advance, as this determines how you will process and handle the Blob data.   https://www.mythdhr.ltd/