Downloading a docx file from binary code file data on browser from ui page button action
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2025 11:11 AM
Hello community ,
I created a Ui page and that Ui page has download button. In the Ui page client script I used glide Ajax and called a function from script include , that script include has a Api call and it returns the binary encoded text data.
I bought this binary encoded data(Ex: W�e(�2�3@Q�w�Z2��) to the Ui page , and now I used blob to convert this binary encoded data to docx , The file is getting downloaded in browser but the content is not displaying , saying could not recognize content. I tried converting the binary code text into base64 value and pass it , but still the data is not appearing in docx file.
So , how to convert a binary encoded data to a docx file without losing the formatting and download it on browser , with a click on UI page.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2025 02:01 AM
Hi @YashwanthKK
To send a ServiceNow attachment to the browser for downloading, you should use the Attachment API, specifically the endpoint that serves the attachment file as a binary stream. This API provides a direct download link for attachments stored in ServiceNow.
The Attachment API endpoint for downloading a file is:
GET /api/now/attachment/{sys_id}/file
Replace {sys_id} with the Sys ID of the attachment you wish to download. When you access this endpoint (either via REST client, browser, or programmatically), it returns the file as a binary stream with appropriate HTTP headers, prompting the browser to download the file.
Example
If your instance is dev12345.service-now.com and the attachment Sys ID is 02c7308f40a97200964f0edb17b6d9d0, the URL would be: https://dev12345.service-now.com/api/now/attachment/02c7308f40a97200964f0edb17b6d9d0/file
Navigating to this URL in a browser (with proper authentication) will initiate the download of the attachment.
Additional Notes:
-
The API returns the file in its original format (e.g., PDF, DOCX, BIN), and the browser handles the download automatically based on the response headers.
-
You can programmatically retrieve the download link using the Attachment API, then present it to users in your UI for direct download.
-
For bulk operations or scripting, you can use tools like
curlto automate downloads using these URLs
Maik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2025 06:40 AM
what's your actual business requirement?
what are you trying to achieve?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Have you found an answer for this? I have a third party system where we want to store our attachment data. We will still keep the sys_attachment record, but not the sys_attachment_doc records. When the user clicks on "View" or "Download" on the 'Manage Attachments' screen on a ticket, it will check to see if the sys_attachment has a reference number (in a custom field). If it does, it will make a call out to the third party system to get the binary equivalent of the file. We would then want it to display the file (without it saving to a new sys_attachment or sys_attachment_doc record) in docviewer (or something like that). If they clicked Download (or directly on the file name), it will do the same steps, but instead of pushing it to docviewer, it will just start a download of the file. I am struggling finding any answers on how this can be accomplished. We have everything set where they click and it calls the API, but we cannot get it further. We also have a couple times where it will act like it downloaded the file, but it will always show as 0 bytes. Thoughts?
If you are wondering why we are going through all of this, it is because we have some very large, required attachments and have gone well beyond our 4TB limit. "Archiving" through ServiceNow does not decrease the database size limit, it just moves stuff out of the 'live' data table to speed up reporting. We need to decrease our database footprint.
