Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

nodejs createwriteStream file image broken

export const streamFile = async (imageUrl: string, imageName: string) => {
	const filename: string = `${process.env.IMG_DIR}/${imageName}`
	const writeFile: fs.WriteStream = fs.createWriteStream(filename)

	const res: request.Request = await request(imageUrl)
	res.pipe(writeFile)
	res.on('data', (chunk: Buffer) => writeFile.write(chunk))
	res.on('complete', () => {
		writeFile.end()
		console.info('Streaming file success')
	})
	res.on('error', () => console.error('Streaming file error'))
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #nodejs #createwriteStream #file #image #broken
ADD COMMENT
Topic
Name
2+1 =