There is a requirement that when I am trying to send images/pdfs files from salesforce to external system, the external system is expecting the content in bytes. I have working code of JAVA but not sure how to convert that to Apex.

Anyone who can show an example of converting Blob of image/pdf files of salesforce to bytes in Apex?

Pasting the working JAVA code

   InputStream inputStream = new FileInputStream(attachmentFilePath);
   long fileSize = new File(attachmentFilePath).length();
   byte[] allBytes = new byte[(int) fileSize];        

Tried to search at different places but don't think that salesforce has any default method to convert blob into byte in Apex.

Thanks in advance.

  • 2
    But what is the purpose of this? What are you going to do with bytes afterwards? I suspect it could be an x-y problem since apex do not have out of the box support of working with bytes/streams, only on the level of blobs/base64 data
    – kurunve
    Oct 25, 2021 at 8:57
  • 1
    As well as the conversion problem, you are likely to hit size limit problems too as Apex limits heap size and HTTP request size. So your problem is an architecture one not a code one, and I think it will need to be tackled that way e.g. signalling the remote system to pull or using an intermediate service or using Salesforce functions or changing the overall approach.
    – Keith C
    Oct 25, 2021 at 10:01
  • @kurunve So I am integrating salesforce with external system. Now external system only accepts the data in bytes when sending images/pdfs. The text/html files are going successfully as those will be string. I tried with base64 & blobs but they don't accept that. Any suggestion on how to achieve it?
    – Jenil
    Oct 25, 2021 at 23:12


You must log in to answer this question.

Browse other questions tagged .