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
660function downloadExample() {
661
662 var html =
663'\n' +
664'\n' +
665' \n' +
666' \n' +
667' \n' +
668' \n' +
669' <div></div>\n' +
670
671' \n' +
672' \n' +
673' \n' +
674' \n' +
675' \n' +
676' \n' +
677' \n' +
678' \n' +
679
680' \n' +
681' \n' +
682'';
683 var file = new Blob([html], {
684 type: 'text/html;charset=UTF-8',
685 encoding: 'UTF-8'
686 });
687 var a = document.createElement('a');
688 a.href = URL.createObjectURL(file);
689 a.download = configs.c + '.html';
690 a.click();
691
692}
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