Home > Blockchain >  TypeError when using writeFile of arraybuffer image data type
TypeError when using writeFile of arraybuffer image data type

Time:12-06

I'm getting an error when I'm trying to write a file with PNG data.

var png = UPNG.encode([data], width, height, 0);
var file = fs.writeFile("mypng.png", png);

Error:

[TypeError: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received an instance of ArrayBuffer

Here is the data and png values:

enter image description here

UPDATE:
The answer provided is correct but I was also needed to pass in the buffer of the UInt8Array.

Before:

var data = UInt8Array(value);
var png = UPNG.encode([data], width, height, 0);
var file = fs.writeFile("mypng.png", png);

After:

var data = UInt8Array(value);
var png = UPNG.encode([data.buffer], width, height, 0);
var buffer = Buffer.from(png);
var file = fs.writeFile("mypng.png", png);

CodePudding user response:

It says you sent an ArrayBuffer but you can only use an instance of a Buffer, TypedArray or DataView

Try converting the ArrayBuffer to a Buffer instance

var data = UInt8Array(value);
var png = UPNG.encode([data.buffer], width, height, 0);
var buffer = Buffer.from(png);
var file = fs.writeFile("mypng.png", buffer);
  • Related