window.ENTITIES={'/api/snippets/javascript/javascript%20-%20run%20file%20download%20(instead%20of%20opening)':[{"result":true,"message":null,"batch":{"type":"javascript","name":"javascript - run file download (instead of opening)","items":[{"id":"jERV01","type":"javascript","name":"JavaScript - run file download (instead of opening)","content":"// Runs file downloading instead of opening.\n//\n// Arguments:\n// srcUrl source URL from where file is downloaded\n// dstFilename file is saved using this filename\n//\nconst downloadFile = (srcUrl, dstFilename) => {\n const a = document.createElement('a');\n a.style.display = 'none';\n a.rel = 'noreferrer noopener'; // <----- optional\n a.href = srcUrl;\n a.download = dstFilename;\n a.target = '_blank'; // <--------------- optional\n document.body.appendChild(a);\n a.click();\n document.body.removeChild(a);\n};\n\n\n// Usage example:\n\ndownloadFile('/path/to/videos?id=123', 'video_name.webm');\n\n\n// See also:\n//\n// 1. https://dirask.com/snippets/HYML-run-file-download-instead-of-opening-D6Qvm1","source":"","author":{"id":"z0mNJD","name":"Walter","avatar":"1629130033631__z0mNJD__w40px_h40px.jpg","points":586,"role":"BASIC"},"creationTime":1675699338000,"updateTime":1695252488000,"removalTime":null}]}}]};