javascripthyperlinkdownloadservicenowservicenow-hrsd

Simple Code to Download a File from a URL when clicked in ServiceNow


I don't have any idea what programming language the ServiceNow application is using. However, I am trying to create a knowledge base in ServiceNow wherein it has link to an excel file, in which stored in a document management software (iManage), then once the use clicks the "download" link, the excel file should be downloaded instead on opening in a new window.

The screenshot attached shows what the knowledge base looks like which showing the "Download" link. enter image description here

This is the code that I have only. I am not sure, but I think, the ServiceNow is using Javascript as programming language.

<p>Job Information Change Mass Upload Template - <a href="https://emea-bm.imanage.work/work/web/r/libraries/EMEA_DMS/folders/EMEA_DMS!9377538?p=1&amp;selectedItem=EMEA_DMS!436639153.1/" target="_blank" rel="noopener noreferrer nofollow">Download</a></p>

With these codes, once I click the "Download" link, it will open in a new window, but it's just literally viewed the file. However, I wanted the file to be downloaded and will not open to view.

I am sorry for asking all your expert help. But I am not really a programmer. Hope you could help me. Thank you so much!


Solution

  • You cannot reprogram an existing website unless you have direct access to the code base, which it sounds like you do not.

    If I am guessing incorrectly and you DO have access to make changes to the site (via their CMS), then all you need to do is add the attribute:

    download="mass_upload_template.xls"
    

    to the a tag.

    If, however, you do not have access to the CMS and you are doing this on your computer, you can write your own browser extension - or use an existing browser extension like Tampermonkey, Greasemonkey or Violentmonkey - to manipulate the existing website on your computer only. (To then copy the functionality over to other computers, you can install Tampermonkey onto those computers and install your script - and those computers will also have the new functionality)

    In fact, I have used Tampermonkey with SNOW exactly in this way, automating all sort of functionality in ServiceNow.

    The key thing in creating the solution in your case is using CSS selectors to identify the p or a tag that you need to modify. Your question does not show the HTML above the desired element, so this is just a guess, but something like this might work:

    // ==UserScript==
    // @name         Force xls file to download
    // @namespace    MyNameSpaceItsMe
    // @match        https://yourdomain.com/*
    // @grant        none
    // ==/UserScript==
    
    'use strict';
    
    const myA = document.querySelector('a[src*="https://emea-bm.imanage.work/work/web/r/libraries/EMEA_DMS/folders/EMEA_DMS"]');
    
    myA.setAttribute('download', 'mass_upload_template.xls');
    myA.setAttribute('target','_self');

    See this other answer (and especially other links inside it) for more information about Tampermonkey, how to install it, how to use it, and what you can do with it.

    References:

    https://stackforgeeks.com/blog/how-to-make-files-download-instead-of-opening-in-browser

    https://davidwalsh.name/download-attribute