Home > Mobile >  Image not downloading
Image not downloading

Time:01-04

I am trying to download the profile picture of a Discord user on Replit. It gets downloaded but is not showing. This is the code:

const request = require('request')
const fs = require('fs')
app.get("/get-picture", (req, res) => {
  // download the picture from the URL
  request.get('https://cdn.discordapp.com/avatars/708947170539339816/cfc4742faa6298e4d8d95070136b6d3a.png', (error, response, body) => {
    // save the picture to a file
    fs.writeFileSync('picture.png', Buffer.from(body, 'binary'))
  })
})

As you can see in the picture, it shows Preview

preview

instead of the image

I tried the code above and I expected to show the picture which is shown when is pressed on the link but it shows only Preview.

CodePudding user response:

You can just use pipe for this. Below code will work fine.

const u = 'https://cdn.discordapp.com/avatars/708947170539339816/cfc4742faa6298e4d8d95070136b6d3a.png'
request(u).pipe(fs.createWriteStream('picture.png'))
  • Related