V
V
Vitaly the Wise2017-09-09 13:36:01
Node.js
Vitaly the Wise, 2017-09-09 13:36:01

How to download a picture from the Internet and send it to dropbox?

Good afternoon. I am writing a simple application for downloading pictures from the Internet and sending them to dropbox. Here is the code:

let body = '';
request
    .get('https://www.w3schools.com/css/img_fjords.jpg')
    .on('data', chunk => {
        body += chunk;
    })
    .on('end', () => {
        console.log(typeof body);

        let contents = new Buffer(body, null);

        dbx.filesUpload({ path: '/w3.jpg', contents })
        .then(resp => {
            console.log(resp);
        })
        .catch(err => {
            console.log(err);
        });
    })
    .on('error', err => {
        console.log('Error: ', err);
    })

Photos uploaded to the cloud only ego can not be opened. Maybe I'm doing something wrong with the encoding. Help me please. Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kwolfy, 2017-09-13
@tapinambur0508

// если указать encoding:null - request вернет body в бинарном формате
request(
  {url:'https://www.w3schools.com/css/img_fjords.jpg', encoding:null}, 
  function (err, res, body) {
    if(err) {
      return console.error('error', err)
    }
  
    dbx.filesUpload({ path: '/w3.jpg', contents: body })
        .then(resp => {
            console.log(resp);
        })
        .catch(err => {
            console.log(err);
        });
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question