You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Mar 10, 2020. It is now read-only.
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.
Copy file name to clipboardExpand all lines: SPEC/FILES.md
+32-4Lines changed: 32 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -181,17 +181,45 @@ If no `callback` is passed, a promise is returned.
181
181
182
182
**Example:**
183
183
184
+
In the browser, assuming `ipfs = new Ipfs(...)`:
185
+
186
+
```js
187
+
let content =ipfs.types.Buffer.from('ABC');
188
+
let results =awaitipfs.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
+
184
197
```JavaScript
185
198
constfiles= [
186
199
{
187
200
path:'/tmp/myfile.txt',
188
-
content:(Buffer or Readable stream)
201
+
content:ipfs.types.Buffer.from('ABC')
189
202
}
190
203
]
191
204
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
0 commit comments