Onclick event for downloading a file on a service portal page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2023 08:12 AM
We ported discussions (questions/answers) from an old forum to ServiceNow. Attachments to those posts were never migrated. The attachments were stored on a file server that was administered by a third party. The third party needs to shutdown the server so we have moved all the files to an internal server and now need to run a script to change the attachment hrefs from https://forum.company.com/...... to https://newserver.company.com.
The issue that we are facing is that there was a PHP script that ran to download the attachments on the old server. <a class="ipsAttachLink" href="https://forum.company.com/....../attachment.php?id=19365" rel="" data-fileid="19365" data-fileext="zip">templex.zip</a>.
Is there a way to add custom javascript to the Service Portal that will run when a link having a class of "ipsAttachLink" is clicked that will download the file?
Thanks for any information that you can provide.
LizB
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2023 08:23 AM
Hi @LizB ,
Yes, it is possible to add custom JavaScript to the Service Portal that will run when a link with a specific class is clicked. Here's an example of how you can achieve this:
- Create a new widget or add the code to an existing widget where you want the functionality to be available.
- Add an event listener for the click event on the link with the class "ipsAttachLink".
- In the event listener function, prevent the default behavior of the link by calling the preventDefault() method on the event object.
- Extract the download URL from the href attribute of the clicked link.
- Create a new anchor element and set its href attribute to the download URL.
- Set the download attribute of the anchor element to the desired filename of the downloaded file.
- Trigger a click event on the anchor element to initiate the download.
Here's an example code snippet that you can use:
document.addEventListener('click', function(event) {
if (event.target.classList.contains('ipsAttachLink')) {
event.preventDefault(); // Prevent default link behavior
var downloadUrl = event.target.href; // Get download URL
var filename = event.target.getAttribute('data-filename'); // Get filename from data attribute
var downloadLink = document.createElement('a'); // Create new anchor element
downloadLink.href = downloadUrl;
downloadLink.download = filename;
downloadLink.click(); // Trigger download
}
});
Note: The data-filename attribute is used to store the desired filename of the downloaded file. Make sure to add this attribute to the link element with the class "ipsAttachLink".
Thanks,
Ratnakar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-01-2023 12:25 PM - edited 05-01-2023 12:30 PM
Hi @Ratnakar7
Thank you for your quick reply. I tried your solution. It appears that the file is opened rather than downloaded.
I'm guessing this is a browser issue but would be interested to know if it is something that can be handled programmatically.
Kind Regards,
LizB
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2024 03:52 AM
Hi, have you found solution for this ? I'm trying to do something similiar but in UI page. I want to download file that is base64 but I don't want to save it to sys attachemnts