10 examples of 'js download file from url' in JavaScript

Every line of 'js download file from url' 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
10function download(fileUrl, filename, callback) {
11 https
12 .get(fileUrl, function(response) {
13 if (
14 response.statusCode > 300 &&
15 response.statusCode < 400 &&
16 response.headers.location
17 ) {
18 if (url.parse(response.headers.location).hostname) {
19 https.get(response.headers.location, function(res) {
20 writeToFile(filename, res, callback);
21 });
22 } else {
23 https
24 .get(
25 url.resolve(
26 url.parse(fileUrl).hostname,
27 response.headers.location
28 ),
29 function(res) {
30 writeToFile(filename, res, callback);
31 }
32 )
33 .on("error", callback);
34 }
35 } else {
36 writeToFile(filename, response, callback);
37 }
38 })
39 .on("error", callback);
40}
120function download(url) {
121 torrents[url] = true;
122 // If the torrent already exists, kick off the downloads of the files, as this
123 // won't happen naturally when the torrent is added (because it already exists!)
124 if(btapp.has('torrent')) {
125 if(btapp.get('torrent').each(function(torrent) {
126 if(torrent.has('properties')) {
127 if(torrent.get('properties').get('download_url') === url) {
128 download_torrent_files(torrent.get('properties'));
129 return;
130 }
131 }
132 }));
133 }
134}
5export async function downloadFromUrl(url: string): Promise {
6 return new Promise((resolve, reject) => {
7 request({
8 method: "GET",
9 url: url,
10 encoding: null,
11 }, (err, res, _body) => {
12 if (err) {
13 LogService.error("utils", "Error downloading file from " + url);
14 LogService.error("utils", err);
15 reject(err);
16 } else if (res.statusCode !== 200) {
17 LogService.error("utils", "Got status code " + res.statusCode + " while calling url " + url);
18 reject(new Error("Error in request: invalid status code"));
19 } else {
20 resolve(res.body);
21 }
22 });
23 });
24}
7export function download(url) {
8 const iframe = document.createElement("iframe");
9 iframe.src = url;
10 iframe.style.display = "none";
11 document.body.appendChild(iframe);
12}
505function download(filename, url) {
506 var a = window.document.createElement('a'),
507 bd = document.querySelector('body');
508 bd.appendChild(a);
509 a.setAttribute('href', url);
510 a.setAttribute('download', filename);
511 a.click();
512 bd.removeChild(a);
513}
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}
142export function loadJS(url) {
143 return new Promise(function(resolve, reject) {
144 const script = document.createElement('script');
145 script.type = 'text/javascript';
146
147 if (script.readyState) { // IE
148 script.onreadystatechange = function() {
149 if (script.readyState === 'loaded' ||
150 script.readyState === 'complete') {
151 script.onreadystatechange = null;
152 resolve({
153 code: 0,
154 msg: 'success: ' + url
155 });
156 }
157 };
158 } else { // Others
159 script.onload = function() {
160 resolve({
161 code: 0,
162 msg: 'success: ' + url
163 });
164 };
165 }
166
167 script.onerror = function() {
168 reject(Error(url + 'load error!'));
169 };
170
171 script.src = url;
172 document.body.appendChild(script);
173 });
174}
16function download(url: string, filename: string) {
17 console.log(`download ${url}...`)
18 return new Promise((resolve) => {
19 request(url, resolve).pipe(fs.createWriteStream(filename))
20 });
21}
28function downloadScript (url, options, onComplete) {
29 var { options, onComplete } = parseParameters(options, undefined, onComplete);
30
31 var d = document, s = document.createElement('script');
32
33 if (window.location.protocol !== 'file:') {
34 s.crossOrigin = 'anonymous';
35 }
36
37 s.async = options.isAsync === undefined ? true : options.isAsync;
38 s.src = url;
39 function loadHandler () {
40 s.parentNode.removeChild(s);
41 s.removeEventListener('load', loadHandler, false);
42 s.removeEventListener('error', errorHandler, false);
43 onComplete && onComplete(null);
44 }
45
46 function errorHandler() {
47 s.parentNode.removeChild(s);
48 s.removeEventListener('load', loadHandler, false);
49 s.removeEventListener('error', errorHandler, false);
50 onComplete && onComplete(new Error(cc.debug.getError(4928, url)));
51 }
52
53 s.addEventListener('load', loadHandler, false);
54 s.addEventListener('error', errorHandler, false);
55 d.body.appendChild(s);
56}
15export function downloadFromUrl(url, filename) {
16 const a = document.createElement('a')
17 a.href = url
18 a.download = filename // Set the file name.
19 a.style.display = 'none'
20 document.body.appendChild(a)
21 a.click()
22 document.body.removeChild(a)
23}

Related snippets