Skip to content

Commit bbe9643

Browse files
authored
fix examples (#234)
1 parent 89776c9 commit bbe9643

File tree

1 file changed

+49
-26
lines changed

1 file changed

+49
-26
lines changed

docs/documentation/upload-download.md

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,23 @@ You can upload and retrieve any `string` or `Uint8Array` data with the `uploadDa
3535

3636
When you download data the return type is the `Data` interface which extends `Uint8Array` with convenience functions like:
3737

38-
- `text()` that converts the bytes into UTF-8 encoded string
38+
- `toUtf8()` that converts the bytes into UTF-8 encoded string
3939
- `hex()` that converts the bytes into **non-prefixed** hex string
4040
- `json()` that converts the bytes into JSON object
4141

4242
```js
43+
import { Bee } from "@ethersphere/bee-js"
44+
45+
const bee = new Bee('http://localhost:1633')
46+
const postageBatchId = "177da0994ed3000d241b183d33758aec42495bf9008fab059f0e3f208f3a1ade"
47+
4348
const result = await bee.uploadData(postageBatchId, "Bee is awesome!")
4449

45-
// prints Swarm hash of the file with which it can be retrieved
46-
// here it is fd79d5e0ebd8407e422f53ce1d7c4c41ebf403be55143900f8d1490560294780
47-
console.log(result.reference)
50+
console.log(result.reference.toHex())
4851

4952
const retrievedData = await bee.downloadData(result.reference)
5053

51-
console.log(retrievedData.text()) // prints 'Bee is awesome!'
54+
console.log(retrievedData.toUtf8()) // prints 'Bee is awesome!'
5255
```
5356

5457
:::info Tip
@@ -60,48 +63,65 @@ A Swarm reference or hash is a 64 character long hex string which is the address
6063
You can also upload files by specifying a filename. When you download the file, `bee-js` will return additional information like the `contentType` or `name` of the file.
6164

6265
```js
66+
import { Bee } from "@ethersphere/bee-js"
67+
68+
const bee = new Bee('http://localhost:1633')
69+
const postageBatchId = "177da0994ed3000d241b183d33758aec42495bf9008fab059f0e3f208f3a1ade"
70+
6371
const result = await bee.uploadFile(postageBatchId, "Bee is awesome!", "textfile.txt")
64-
const retrievedFile = await bee.downloadFile(result.reference)
72+
const retrievedFile = await bee.downloadFile(result.reference.toHex())
6573

6674
console.log(retrievedFile.name) // prints 'textfile.txt'
67-
console.log(retrievedFile.contentType) // prints 'application/octet-stream'
68-
console.log(retrievedFile.data.text()) // prints 'Bee is awesome!'
75+
console.log(retrievedFile.contentType) // prints 'application/x-www-form-urlencoded'
76+
console.log(retrievedFile.data.toUtf8()) // prints 'Bee is awesome!'
6977
```
7078

7179
In browsers, you can directly upload using the [`File` interface](https://developer.mozilla.org/en-US/docs/Web/API/File). The filename is taken from the `File` object itself, but can be overwritten through the second argument of the `uploadFile` function.
7280

7381

7482
```js
75-
const file = new File(["foo"], "foo.txt", { type: "text/plain" })
83+
import { Bee } from "@ethersphere/bee-js"
84+
import fs from 'fs'
7685

77-
const postageBatchId = await bee.createPostageBatch("100", 17)
78-
const result = await bee.uploadFile(postageBatchId, file)
79-
const retrievedFile = await bee.downloadFile(result.reference)
86+
const bee = new Bee('http://localhost:1633')
87+
const postageBatchId = "177da0994ed3000d241b183d33758aec42495bf9008fab059f0e3f208f3a1ade"
8088

81-
console.log(retrievedFile.name) // prints 'foo.txt'
82-
console.log(retrievedFile.contentType) // prints 'text/plain'
83-
console.log(retrievedFile.data.text()) // prints 'foo'
89+
// Read the file content
90+
const fileContent = fs.readFileSync("./textFile.txt", "utf8")
91+
92+
// Upload the file content with a name
93+
const result = await bee.uploadFile(postageBatchId, fileContent, "textfile.txt")
94+
95+
// Download the file
96+
const retrievedFile = await bee.downloadFile(result.reference)
97+
console.log(retrievedFile.name) // prints 'textfile.txt'
98+
console.log(retrievedFile.contentType) // should print 'application/x-www-form-urlencoded
99+
console.log(retrievedFile.data.toUtf8()) // prints the file content
84100
```
85101

86102
### Files and Directories
87103

88104
In browsers, you can easily upload an array of `File` objects coming from your form directly with [`FileList`](https://developer.mozilla.org/en-US/docs/Web/API/FileList). If the files uploaded through `uploadFiles` have a relative path, they are added relative to this filepath. Otherwise, the whole structure is flattened into single directory.
89105

90106
```js
107+
import { Bee } from "@ethersphere/bee-js"
108+
import fs from 'fs'
109+
110+
const bee = new Bee('http://localhost:1633')
111+
const postageBatchId = "177da0994ed3000d241b183d33758aec42495bf9008fab059f0e3f208f3a1ade"
91112
const foo = new File(["foo"], "foo.txt", { type: "text/plain" })
92113
const bar = new File(["bar"], "bar.txt", { type: "text/plain" })
93114

94-
const postageBatchId = await bee.createPostageBatch("100", 17)
95115
const result = await bee.uploadFiles(postageBatchId, [ foo, bar ]) // upload
96116

97-
const rFoo = await bee.downloadFile(result.reference, './foo.txt') // download foo
98-
const rBar = await bee.downloadFile(result.reference, './bar.txt') // download bar
117+
const Foo = await bee.downloadFile(result.reference, './foo.txt') // download foo
118+
const Bar = await bee.downloadFile(result.reference, './bar.txt') // download bar
99119

100-
console.log(rFoo.data.text()) // prints 'foo'
101-
console.log(rBar.data.text()) // prints 'bar'
120+
console.log(Foo.data.toUtf8()) // prints 'foo'
121+
console.log(Bar.data.toUtf8()) // prints 'bar'
102122
```
103123

104-
In NodeJS, you may utilize the `uploadFilesFromDirectory` function, which takes the directory path as input and uploads all files in that directory. Let's assume we have the following file structure:
124+
You may also utilize the `uploadFilesFromDirectory` function, which takes the directory path as input and uploads all files in that directory. Let's assume we have the following file structure:
105125

106126
```sh
107127
.
@@ -111,14 +131,17 @@ In NodeJS, you may utilize the `uploadFilesFromDirectory` function, which takes
111131
```
112132

113133
```js
114-
const postageBatchId = await bee.createPostageBatch("100", 17)
134+
import { Bee } from "@ethersphere/bee-js"
135+
import fs from 'fs'
115136

137+
const bee = new Bee('http://localhost:1633')
138+
const postageBatchId = "177da0994ed3000d241b183d33758aec42495bf9008fab059f0e3f208f3a1ade"
116139
const result = await bee.uploadFilesFromDirectory(postageBatchId, './') // upload recursively current folder
117140

118-
const rFoo = await bee.downloadFile(result.reference, './foo.txt') // download foo
119-
const rBar = await bee.downloadFile(result.reference, './dir/bar.txt') // download bar
141+
const Foo = await bee.downloadFile(result.reference, './foo.txt') // download foo
142+
const Bar = await bee.downloadFile(result.reference, './dir/bar.txt') // download bar
120143

121-
console.log(rFoo.data.text()) // prints 'foo'
122-
console.log(rBar.data.text()) // prints 'bar'
144+
console.log(Foo.data.toUtf8()) // prints 'foo'
145+
console.log(Bar.data.toUtf8()) // prints 'bar'
123146
```
124147

0 commit comments

Comments
 (0)