source: trip-planner-front/node_modules/needle/examples/upload-image.js@ 6c1585f

Last change on this file since 6c1585f was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1010 bytes
Line 
1var needle = require('../'),
2 path = require('path');
3
4var image = 'https://upload.wikimedia.org/wikipedia/commons/a/af/Tux.png';
5
6function upload(obj, cb) {
7 console.log('Uploading image...');
8
9 var url = 'http://deviantsart.com';
10
11 var opts = {
12 timeout: 10000,
13 follow: 3,
14 multipart: true
15 };
16
17 var params = {
18 file: obj
19 }
20
21 needle.post(url, params, opts, function(err, resp) {
22 if (err || !resp.body.match('url'))
23 return cb(err || new Error('No image URL found.'))
24
25 cb(null, JSON.parse(resp.body).url)
26 })
27}
28
29function download(url, cb) {
30 console.log('Getting ' + url);
31 needle.get(url, function(err, resp) {
32 if (err) throw err;
33
34 cb(null, resp.body);
35 })
36}
37
38////////////////////////////////////////
39// ok, now go.
40
41download(image, function(err, buffer) {
42 if (err) throw err;
43
44 var obj = { buffer: buffer, content_type: 'image/png' };
45
46 upload(obj, function(err, url) {
47 if (err) throw err;
48
49 console.log('Image uploaded to ' + url);
50 })
51})
Note: See TracBrowser for help on using the repository browser.