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

Every line of 'jquery 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
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}
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}
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}
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}
14function download(url, w, progress = () => {}) {
15 return new Promise((resolve, reject) => {
16 let protocol = /^https:/.exec(url) ? https : http
17 progress(0)
18 protocol
19 .get(url, res1 => {
20 protocol = /^https:/.exec(res1.headers.location) ? https : http
21 protocol
22 .get(res1.headers.location, res2 => {
23 const total = parseInt(res2.headers['content-length'], 10)
24 let completed = 0
25 res2.pipe(w)
26 res2.on('data', data => {
27 completed += data.length
28 progress(completed / total)
29 })
30 res2.on('progress', progress)
31 res2.on('error', reject)
32 res2.on('end', () => resolve(w.path))
33 })
34 .on('error', reject)
35 })
36 .on('error', reject)
37 })
38}
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}
1function download(file, ext) {
2 var d = new Date();
3 var n = d.getTime();
4 if (window.navigator.msSaveOrOpenBlob) // IE10+
5 window.navigator.msSaveOrOpenBlob(file, n + ext);
6 else { // Others
7 var a = document.createElement("a"),
8 url = URL.createObjectURL(file);
9 a.href = url;
10 a.download = n + ext;
11 document.body.appendChild(a);
12 a.click();
13 setTimeout(function() {
14 document.body.removeChild(a);
15 window.URL.revokeObjectURL(url);
16 }, 0);
17 }
18}
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}
6function getURL(url, onLoad) {
7 const xmlhttp = new XMLHttpRequest();
8
9 if (!url) {
10 onLoad(null);
11 return;
12 }
13
14 xmlhttp.onreadystatechange = () => {
15 if (xmlhttp.readyState === requestDone) {
16 if (xmlhttp.status === 200) {
17 let data = null;
18 try {
19 data = JSON.parse(xmlhttp.responseText);
20 } catch (e) {
21 // eslint-disable-next-line
22 console.log('Loading', url,
23 'Expecting JSON, instead got', xmlhttp.responseText);
24 }
25 onLoad(data);
26 } else {
27 // eslint-disable-next-line
28 console.log('Loading', url, 'expected 200, got', xmlhttp.status, xmlhttp.responseText);
29 }
30 }
31 };
32 xmlhttp.open('GET', url, true);
33 xmlhttp.send();
34}

Related snippets