Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

add file does not work in browser (TypeError: target._set is not a function) #344

Closed
@nycoliver

Description

@nycoliver

Same code as examples/browser-add but adding a file instead of text. It works with text but when adding a file I get index.js:1281 Uncaught TypeError: target._set is not a function

Maybe I am incorrectly forming the buffer? But even the files add code from ipfs-webui throws the same error.

index.html

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8"/>
    <title>JS IPFS API - Example - Browser - Add</title>
    <script src="buffer.js"></script> <!-- feross buffer -->
    <script src="ipfs.js"></script> <!-- js-ipfs-api/dist/index.js -->
    <script src="index.js"></script> <!-- see below -->
  </head>
  <body>
    <h1>JS IPFS API - Add file from the browser</h1>
    <input type="file" id="source">
    <button id="store">create in ipfs</button>
    <div><div>found in ipfs:</div>
      <div id="hash">[ipfs hash]</div>
      <div id="content">[ipfs content]</div>
    </div>
  </body>
</html>

index.js

var ipfs = window.IpfsApi()

function store () {
  const file = document.getElementById('source').files[0];
  const reader = new FileReader();
  reader.onload = function() {

    const buffer = new window.buffer.Buffer(reader.result);
    ipfs.add(buffer, function (err, res) {
      if (err || !res) {
        return console.error('ipfs add error', err, res)
      }

      res.forEach(function (file) {
        console.log('successfully stored', file)
        display(file.path)
      })
    })
  };
  reader.readAsArrayBuffer(file);
}

document.addEventListener('DOMContentLoaded', function() {
  document.getElementById('store').onclick = store
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions