0

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);

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

4 Comments

I have tried that and it gets rid of the error but the png is blank. It does show the correct size in the Finder. If that's the case then it must be the encoder?
@1.21gigawatts my first thought was permissions to the server, it could be blank if it can't write but you said it's showing the right file size so it must be an issue with corrupted data, have you tried different pngs?
it was figured out. you are correct to get the buffer from the png data. data was a Uint8array and I needed to pass in the the buffer of that data so data.buffer and it worked.
I'm marking this as the correct answer because it doesn't work without the code you've shown

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.