10 examples of 'download image from url javascript' in JavaScript

Every line of 'download image from url javascript' 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
67function downloadImage(location: DownloadFromWeb, callback: IDownloadCallback): void {
68 async.waterfall([
69 (next: IDownloadCallback) => {
70 // work out where to download the file to
71 tmp.file({ keep : true, discardDescriptor : true, prefix : 'dl-', postfix : '.png' }, (err, tmppath) => {
72 next(err, tmppath);
73 });
74 },
75 (tmpFilePath: string, next: IDownloadCallback) => {
76 runResizeFunction({ url : location.url })
77 .then((response) => {
78 if (response.statusCode !== 200) {
79 const errWithLocation: any = new Error(response.body.error) as unknown;
80 errWithLocation.location = location;
81 errWithLocation.statusCode = response.statusCode;
82 return next(errWithLocation, tmpFilePath);
83 }
84 fs.writeFile(tmpFilePath, response.body, 'base64', (err) => {
85 next(err, tmpFilePath);
86 });
87 }).catch((err) => {
88 const errWithLocation: any = new Error('Unable to download image from ' +
89 getHostFromUrl(location.url)) as unknown;
90 errWithLocation.location = location;
91 errWithLocation.statusCode = 500;
92 return next(errWithLocation, tmpFilePath);
93 });
94 },
95 ], (err?: Error | null, downloadedPath?: string) => {
96 if (err) {
97 // console.log('Failed to download', err, location);
98 }
99 callback(err, downloadedPath);
100 });
101}
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

117function downloadImageAsync(url, callback) {
118 let stream = Gio.file_new_for_uri(url);
119
120 stream.read_async(GLib.PRIORITY_DEFAULT, null, Lang.bind(this, function(src, res) {
121 let inputStream;
122 try {
123 inputStream = stream.read_finish(res);
124 } catch (e) {
125 callback(false);
126 return;
127 }
128
129 let dirPath = GLib.get_user_cache_dir();
130
131 let basename = stream.get_basename();
132 let path = GLib.build_filenamev([dirPath, "/bolso/", basename]);
133
134 let out = Gio.file_new_for_path(path);
135 out.replace_async(null, false, Gio.FileCreateFlags.NONE, GLib.PRIORITY_DEFAULT, null,
136 Lang.bind(this, function(src, res) {
137 let outputStream = out.replace_finish(res);
138
139 outputStream.splice_async(inputStream,
140 Gio.OutputStreamSpliceFlags.CLOSE_SOURCE | Gio.OutputStreamSpliceFlags.CLOSE_TARGET,
141 GLib.PRIORITY_DEFAULT, null, Lang.bind(this, function(src, res) {
142 try {
143 outputStream.splice_finish(res);
144 } catch (e) {
145 return;
146 }
147
148 callback(path);
149 }));
150 }));
151 }));
152}
42downloadImage (url, filename) {
43 let imagePath = this.userProfilePath + filename + '.png'
44 let options = {
45 url: url,
46 dest: imagePath,
47 done: function(err, filename, image) {
48 if (err) {
49 throw err;
50 }
51
52 console.log('File saved to', filename);
53 },
54 }
55
56 imageDownloader(options);
57 this.set('image', imagePath)
58}
10downloadImageURL(url) {
11 const filename = [this.getDownloadFolder(), this.getDownloadFilename(url)].filter(p => !!p).join("/");
12 this.module.downloads.setShelfEnabled(false);
13 const saveAs = false;
14 return new Promise(resolve => this.module.downloads.download({url, filename, saveAs}, () => {
15 resolve();
16 setTimeout(() => this.module.downloads.setShelfEnabled(true), 1000);
17 }));
18}
34export function getDataUrlfromUrl(src, callback) {
35 const img = new Image();
36 img.crossOrigin = 'Anonymous';
37 img.onload = function () {
38 const canvas = document.createElement('CANVAS');
39 const ctx = canvas.getContext('2d');
40 let dataURL;
41 canvas.height = this.height;
42 canvas.width = this.width;
43 ctx.drawImage(this, 0, 0);
44 dataURL = canvas.toDataURL();
45 callback(dataURL);
46 };
47 img.src = src;
48 if (img.complete || img.complete === undefined) {
49 img.src = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==';
50 img.src = src;
51 }
52}
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}
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}
115function readURL(input, img_id) {
116 if (input.files && input.files[0]) {
117 var reader = new FileReader();
118 reader.onload = function (e) {
119 $(img_id).attr('src', e.target.result);
120 };
121 reader.readAsDataURL(input.files[0]);
122 }
123}
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}
5export async function downloadFromUrl(url: string): Promise<any> {
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}

Related snippets