0

I try to push a file to the IPFS, and I have converted to the Buffer. I got this error " content.once is not a function". I am using this library in node.

var Buffer = require('buffer/').Buffer;
const doc = new jsPDF();
doc.fromHTML('test',10,10);
var covnertedBuffer = Buffer.from(doc.output('arraybuffer');

Then, I take the convertedBuffer and pass it to the IPFS api.

Any idea?

Updated test:

I have successfully pushed a file to the IPFS via the API with this code below.

const filename = '/home/administrator/Downloads/5HP8LWKHLV.pdf';
 this.ipfsApi = ipfsApi('localhost', '5001');
    let readablestream = fs.createReadStream(filename);
    readablestream.on('readable', () => {
      let result = readablestream.read();
      console.log(result);
      if (result) {
        this.ipfsApi.files.add(result, function(err, files) {
          if (err) {
            res.json('err');
            console.log(err);
          }
          res.json(files);
        });
      }
    });

But, when I get the arrayBuffer from the doc.output and convert to the Buffer object and push to the IPFS and it failed. Please see below.

 var _buffer = Buffer.from(req.buffer);
    console.log('Converted to buffer:' + _buffer);
    this.ipfsApi = ipfsApi('localhost', '5001');
    this.ipfsApi.files.add(_buffer, function(err, files) {
      if (!err) {
        res.status(500);
        console.log(err);
      } else {

        res.json(files);
        res.status(200);
      }
    });

Thank you

DavidB
  • 313
  • 1
  • 8
  • 23
  • What is content? It likely isn't what you think it is. That is an eventemitter. – Dan D. Apr 05 '18 at 07:30
  • @DanD. - I have updated above. Any clues? – DavidB Apr 05 '18 at 07:34
  • The first method I pas in with the physical file and it works fine, the second method I just pass in the arraybuffer generated from the doc.output('arraybuffer') and convert it to the Buffer object. The IPFS add method returns with the undefined object but the hash key. – DavidB Apr 06 '18 at 05:30

1 Answers1

2

Adding Buffer.from(your_buffer) to your buffer before doing ipfs push works.

ipfs.files.add(Buffer.from(put_your_buffer_here), (error, result) => {
      if(error) {
        console.error(error)
        return
      }
      console.log("upload is successful");

  });