Trigger download of the image file in a hacky way

Apparently this is the Recommended Approach for doing this - you create an <a> element,
set its attributes, then temporarily add it to the page and click() it.

The downloads.download() api we found earlier is exclusive to Chrome Apps.
mike/download-frame-2
Mike Lang 2 years ago
parent 8a0705c927
commit 669cc47e0e

@ -470,6 +470,16 @@ function downloadFrame() {
const url = `/frame/${globalStreamName}/source.png?timestamp=${wubloaderTimeFromDateTime(dateTime)}`; const url = `/frame/${globalStreamName}/source.png?timestamp=${wubloaderTimeFromDateTime(dateTime)}`;
// Avoid : as it causes problems on Windows // Avoid : as it causes problems on Windows
const filename = `${dateTime.toFormat("yyyy-LL-dd'T'HH-mm-ss.SSS")}.png`; const filename = `${dateTime.toFormat("yyyy-LL-dd'T'HH-mm-ss.SSS")}.png`;
// TODO REPLACE WITH CORRECT CALL triggerDownload(url, filename);
console.log(`Would download ${url} as ${filename}`); }
function triggerDownload(url, filename) {
// URL must be same-origin.
const link = document.createElement('a');
link.setAttribute('download', filename);
link.href = url;
link.setAttribute('target', '_blank');
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
} }

Loading…
Cancel
Save