5 examples of 'javascript file download example' in JavaScript

Every line of 'javascript file download example' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
660function downloadExample() {
661
662 var html =
663'<!DOCTYPE html>\n' +
664'<html style="height: 100%">\n' +
665' <head>\n' +
666' <meta charset="utf-8">\n' +
667' </head>\n' +
668' <body style="height: 100%; margin: 0">\n' +
669' <div id="container" style="height: 100%"></div>\n' +
670
671' <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js"></script>\n' +
672' <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/echarts-gl/dist/echarts-gl.min.js"></script>\n' +
673' <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/echarts-stat/dist/ecStat.min.js"></script>\n' +
674' <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/echarts/dist/extension/dataTool.min.js"></script>\n' +
675' <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/echarts/map/js/china.js"></script>\n' +
676' <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/echarts/map/js/world.js"></script>\n' +
677' <script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=xfhhaTThl11qYVrqLZii6w8qE5ggnhrY&__ec_v__=20190126"></script>\n' +
678' <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/echarts/dist/extension/bmap.min.js"></script>\n' +
679
680' <script type="text/javascript">\n' +
681'var dom = document.getElementById("container");\n' +
682'var myChart = echarts.init(dom);\n' +
683'var app = {};\n' +
684
685'option = null;\n' +
686gb.editor.getValue() + ';\n' +
687
688'if (option && typeof option === "object") {\n' +
689' myChart.setOption(option, true);\n' +
690'}\n' +
691' </script>\n' +
692' </body>\n' +
693'</html>';
694 var file = new Blob([html], {
695 type: 'text/html;charset=UTF-8',
696 encoding: 'UTF-8'
697 });
698 var a = document.createElement('a');
699 a.href = URL.createObjectURL(file);
700 a.download = configs.c + '.html';
701 a.click();
702
703}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

237function download(content, filename, contentType)
238{
239 if(!contentType) contentType = 'application/octet-stream';
240 var a = document.createElement('a');
241 var blob = new Blob([content], {'type':contentType});
242 a.href = window.URL.createObjectURL(blob);
243 a.download = filename;
244 a.click();
245}
1export function downloadString(
2 content: string,
3 fileName: string,
4 type = 'text/plain'
5) {
6 const anchor = document.createElement('a')
7 anchor.style.display = 'none'
8 document.body.appendChild(anchor)
9 anchor.href = window.URL.createObjectURL(new Blob([content], { type }))
10 anchor.setAttribute('download', fileName)
11 anchor.click()
12 window.URL.revokeObjectURL(anchor.href)
13 document.body.removeChild(anchor)
14}
10function download(filename, text) {
11 const blob = new Blob([text], { type: 'text/plain;charset=utf-8' });
12
13 // release previous url in case of multiple uses in one session
14 if (urls.download) {
15 window.URL.revokeObjectURL(urls.download);
16 }
17
18 // create an addressable version of the blob
19 urls.download = window.URL.createObjectURL(blob);
20
21 const element = document.createElement('a');
22 element.setAttribute('href', urls.download);
23 element.setAttribute('download', filename);
24 element.style.display = 'none';
25 document.body.appendChild(element);
26 element.click();
27 document.body.removeChild(element);
28}
22download() {
23 var fileName = this.imageService.fileName;
24 this.embedService.embeddedCanvas.toBlob(function (blob) {
25 download(blob, fileName, 'image/png');
26 })
27}

Related snippets