I'm reading a file using Node and attempting to send it as a multi part MIME post, but having problems with the file appearing corrupt at the receiving end.
I read the file and prepare it for sending like so:
fs.readFile("before.png", function(err,data) {
// Snip
content += data.slice(0,data.length);
The problem is that something seems to padding the stream. See images below comparing the before.png source file with the file as received at the destination. The sequences of EF BF BD indicate that .slice() is not giving me the underlying bytes, possibly something coerced to UTF-8 encoding.
I've also tried getting the bytes via toString(), but no beans there. I still see corruption in the uploaded file.
// content += data.toString() // UTF-8 default
content += data.toString('binary')
I suspect the default toString() is also coercing the binary file to UTF-8 too, but would have expected 'binary' to give me the underlying byte stream?
Edit in response to Brad. I tried concattin'g but I still need to translate my object back to a string, at which point the UTF-8 characters seem to reappear in the stream.
contentToSend = Buffer.concat([ header, data, footer ] );
this.oauth.post( endpoint, accessToken, accessTokenSecret, contentToSend.toString(), contentType, function( x, y, z ) {