1function download(filename, text) {
2 var element = document.createElement('a');
3 element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
4 element.setAttribute('download', filename);
5
6 element.style.display = 'none';
7 document.body.appendChild(element);
8
9 element.click();
10
11 document.body.removeChild(element);
12}
13
14// Start file download.
15download("hello.txt","This is the content of my file :)");
16
1import urllib.request
2
3print('Beginning file download with urllib2...')
4
5url = 'http://i3.ytimg.com/vi/J---aiyznGQ/mqdefault.jpg'
6urllib.request.urlretrieve(url, '/Users/scott/Downloads/cat.jpg')
7
1function download(link) {
2 var element = document.createElement('a');
3 element.setAttribute('href', link);
4
5 element.style.display = 'none';
6 document.body.appendChild(element);
7
8 element.click();
9
10 document.body.removeChild(element);
11}
1$('a#someID').attr({target: '_blank',
2 href : 'http://localhost/directory/file.pdf'});
1let data = JSON.stringify([{email: "test@domain.com", name: "test"}, {email: "anothertest@example.com", name: "anothertest"}]);
2
3let type = "application/json", name = "testfile.json";
4downloader(data, type, name)
5
6function downloader(data, type, name) {
7 let blob = new Blob([data], {type});
8 let url = window.URL.createObjectURL(blob);
9 downloadURI(url, name);
10 window.URL.revokeObjectURL(url);
11}
12
13function downloadURI(uri, name) {
14 let link = document.createElement("a");
15 link.download = name;
16 link.href = uri;
17 link.click();
18}
19 Run code snippet