[Edit]
+
0
-
0

JavaScript - run file download (instead of opening)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// Runs file downloading instead of opening. // // Arguments: // srcUrl source URL from where file is downloaded // dstFilename file is saved using this filename // const downloadFile = (srcUrl, dstFilename) => { const a = document.createElement('a'); a.style.display = 'none'; a.rel = 'noreferrer noopener'; // <----- optional a.href = srcUrl; a.download = dstFilename; a.target = '_blank'; // <--------------- optional document.body.appendChild(a); a.click(); document.body.removeChild(a); }; // Usage example: downloadFile('/path/to/videos?id=123', 'video_name.webm'); // See also: // // 1. https://dirask.com/snippets/HYML-run-file-download-instead-of-opening-D6Qvm1