Handling document attachments in Business Central, especially via the /documentAttachments
endpoint, can be unexpectedly fragile. That “Read called with an open stream or textreader” error usually points to how the file stream is being processed before the API call. Even if you’re encoding the file to base64, the platform may still interpret the stream as open if it hasn’t been fully resolved or finalized.
Your current approach using arrayBuffer → Uint8Array → binaryString → btoa()
is technically valid, but Axios doesn’t always guarantee that the stream is closed in a way Business Central expects. This is especially true when working with binary content in browser environments.
One workaround that’s proven reliable is exposing Page 30080 (Document Attachments) as a custom API page. This lets you bypass the stream error entirely and gives you full control over how attachments are linked to Sales Orders. You can publish it via Web Services and POST directly to it.
Another route is using Power Automate’s HTTP connector instead of the native Business Central connector. It avoids some of the serialization quirks and lets you send the payload in a more predictable format. If you’re open to external storage, uploading the file to Azure Blob or OneDrive first and then linking it in Business Central is also a clean workaround.
Lastly, if you’re sticking with base64, try using FileReader.readAsDataURL()
instead of manually building the binary string. It ensures the stream is closed and the encoding is padded correctly. Just strip the prefix (data:application/pdf;base64,
) before sending.
Helpful Reference
Josh Anglesea – Business Central Attachments via Standard API
Microsoft Docs – Attachments API Reference
Saurav Dhyani – Base64 Conversion in Business Central
Power Platform Community – HTTP Connector Workaround
Dynamics Community – Stream Error Discussion
If you find this helpful, feel free to upvote this answer.
Cheers
Jeffrey