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

Commit e5d90e2

Browse files
klueqAlan Shaw
authored and
Alan Shaw
committed
docs: add note about ipfs.types.Buffer (#373)
I've spent an hour trying to figure out how to do `ipfs add` in the browser: docs describe the "Buffer" part really vaguely and don't mention that `ipfs.files.add` in fact creates a directory object unless used with a correctly created buffer. It works with an incorrectly created buffer, but creates a directory with auto-generated name: ```js let data = new Uint8Array([0x41, 0x42, 0x43]); let results = await ipfs.files.add(data); ``` Here `results` is ```json [ { "path": "QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn", "hash": "QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn", "size": 4 } ] ``` Now open this hash in `ipfs.io` and try to figure out wtf that is and why size is 4. The `ipfs.types.Buffer` was the key.
1 parent 7a931c1 commit e5d90e2

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

SPEC/FILES.md

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,45 @@ If no `callback` is passed, a promise is returned.
181181

182182
**Example:**
183183

184+
In the browser, assuming `ipfs = new Ipfs(...)`:
185+
186+
```js
187+
let content = ipfs.types.Buffer.from('ABC');
188+
let results = await ipfs.files.add(content);
189+
let hash = results[0].hash; // "Qm...WW"
190+
```
191+
192+
Now [ipfs.io/ipfs/Qm...WW](https://ipfs.io/ipfs/QmNz1UBzpdd4HfZ3qir3aPiRdX5a93XwTuDNyXRc6PKhWW)
193+
returns the "ABC" string.
194+
195+
The following allows you to add multiple files at once. Note that intermediate directories in file paths will be automatically created and returned in the response along with files:
196+
184197
```JavaScript
185198
const files = [
186199
{
187200
path: '/tmp/myfile.txt',
188-
content: (Buffer or Readable stream)
201+
content: ipfs.types.Buffer.from('ABC')
189202
}
190203
]
191204

192-
ipfs.files.add(files, function (err, files) {
193-
// 'files' will be an array of objects containing paths and the multihashes of the files added
194-
})
205+
const results = await ipfs.files.add(files);
206+
```
207+
208+
The `results` array:
209+
210+
```json
211+
[
212+
{
213+
"path": "tmp",
214+
"hash": "QmWXdjNC362aPDtwHPUE9o2VMqPeNeCQuTBTv1NsKtwypg",
215+
"size": 67
216+
},
217+
{
218+
"path": "/tmp/myfile.txt",
219+
"hash": "QmNz1UBzpdd4HfZ3qir3aPiRdX5a93XwTuDNyXRc6PKhWW",
220+
"size": 11
221+
}
222+
]
195223
```
196224

197225
A great source of [examples][] can be found in the tests for this API.

0 commit comments

Comments
 (0)