summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/lib/utils/downloader.js
blob: 2297f5f90ce61580a6894864878aca66b93d139d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
 * Helper function to trigger a download.
 *
 * - If the `fileName` is `_blank` it will open the file in a new tab.
 * - If `fileData` is provided, it will inline the content and use data URLs to
 *   download the file. In this case the `url` property will be ignored. Please
 *   note that `fileData` needs to be Base64 encoded.
 */
export default ({ fileName, url, fileData }) => {
  let href = url;

  if (fileData) {
    href = `data:text/plain;base64,${fileData}`;
  }

  const anchor = document.createElement('a');
  anchor.download = fileName;
  anchor.href = href;
  anchor.click();
};