Currently trying to download image from GitHub locally. Everything seems to work, the fetch goes through with a 200 OK
response, however, I don't understand how to store image itself:
const rawGitLink = "https://raw.githubusercontent.com/cardano-foundation/CIPs/master/CIP-0001/CIP_Flow.png"
const folder = "/Folder"
const imageName = "/Test"
const imageResponse = await axios.get(rawGitLink)
fs.writeFileSync(___dirname folder imageName, imageResponse, (err) => {
//Error handling
}
)
CodePudding user response:
Four problems had to be fixed:
- Image name must include png format for this case
- The response must be in the correct format as a buffer for an image
- You must write the response data and not the object itself
__dirname
only needs two underscores
const rawGitLink = "https://raw.githubusercontent.com/cardano-foundation/CIPs/master/CIP-0001/CIP_Flow.png"
const folder = "/Folder"
const imageName = "/Test.png"
const imageResponse = await axios.get(rawGitLink, { responseType: 'arraybuffer' });
fs.writeFileSync(__dirname folder imageName, imageResponse.data)
CodePudding user response:
Axios returns a special object: https://github.com/axios/axios#response-schema
let {data} = await axios.get(...)
await fs.writeFile(filename, data) // you can use fs.promises instead of sync
As @Leau said you should include the extension on the filename
Another sugestion is to use the path
module to create the filename:
filename = path.join(__dirname, "/Folder", "Test.png")