@@ -61,10 +61,10 @@ const bee = new Bee("http://localhost:1633")
6161// 2) Buy storage (postage stamp batch) for this session
6262const batchId = await bee .buyStorage (Size .fromGigabytes (1 ), Duration .fromDays (1 ))
6363
64- // 3) Read the file from disk
64+ // 3) Read the file from disk as bytes
6565const data = await readFile (" ./hello.txt" )
6666
67- // 4) Upload with a filename and content type; capture the reference
67+ // 4) Upload the bytes with a filename and content type; capture the reference
6868const { reference } = await bee .uploadFile (batchId, data, " hello.txt" , { contentType: " text/plain" })
6969console .log (" Uploaded reference:" , reference .toHex ())
7070
@@ -92,31 +92,32 @@ When working with browsers you can use the [`File` interface](https://developer.
92922 . Buy storage and get postage stamp batch ID:
9393 ` const batchId = await bee.buyStorage(Size.fromGigabytes(1), Duration.fromDays(1)) `
9494
95- 3 . Grab a ` File ` from ` <input type="file" id="file"> ` :
96- ` const fileInput = document.querySelector("#file") as HTMLInputElement; `
97- ` const selected = fileInput.files![0] `
95+ 3 . Create a ` File ` object:
96+ ` const file = new File(["Hello Swarm!"], "hello.txt", { type: "text/plain" }) `
9897
99984 . Use batch ID to upload → get reference:
100- ` const { reference } = await bee.uploadFile(batchId, selected ) `
99+ ` const { reference } = await bee.uploadFile(batchId, file ) `
101100
1021015 . Download by reference:
103102 ` const downloaded = await bee.downloadFile(reference) `
104103
105104``` js
105+
106106import { Bee , Size , Duration } from " @ethersphere/bee-js"
107107
108+ // 1) Connect to your Bee node HTTP API
108109const bee = new Bee (" http://localhost:1633" )
109110
110- // 1 ) Buy storage
111+ // 2 ) Buy storage (postage stamp batch) for this session
111112const batchId = await bee .buyStorage (Size .fromGigabytes (1 ), Duration .fromDays (1 ))
112113console .log (" Batch ID:" , String (batchId))
113114
114- // 2 ) Upload a single file created in code
115+ // 3 ) Upload a single file created in code
115116const file = new File ([" Hello Swarm!" ], " hello.txt" , { type: " text/plain" })
116117const { reference } = await bee .uploadFile (batchId, file)
117118console .log (" Reference:" , String (reference))
118119
119- // 3 ) Download and print name + contents
120+ // 4 ) Download and print name + contents
120121const downloaded = await bee .downloadFile (reference)
121122console .log (downloaded .name ) // "hello.txt"
122123console .log (file .contentType ) // "text/plain"
@@ -134,28 +135,48 @@ Use **`uploadFiles`** (browser-only). It accepts `File[]`/`FileList`. When using
1341352 . Buy storage and get postage stamp batch ID:
135136 ` const batchId = await bee.buyStorage(Size.fromGigabytes(1), Duration.fromDays(1)) `
136137
137- 3 . Select files/folder via ` <input type="file" webkitdirectory multiple id="dir"> `
138- ` const files = Array.from(document.querySelector("#dir")!.files!) `
138+ 3 . Create files for upload:
139+ ``` js
140+ const files = [
141+ new File ([" <h1>Hello Swarm</h1>" ], " index.html" , { type: " text/html" }),
142+ new File ([" body{font-family:sans-serif}" ], " assets/main.css" , { type: " text/css" })
143+ ]
144+ ```
139145
1401464 . Upload multiple files (collection) → get collection reference
141147 ` const res = await bee.uploadFiles(batchId, files) `
142148
143- 5 . Download a specific file by its relative path
149+ 5 . Download files by relative path
144150 ` const logo = await bee.downloadFile(res.reference, "images/logo.png") `
145151
146152``` js
147153import { Bee , Size , Duration } from " @ethersphere/bee-js"
148154
155+ // 1. Initialize a Bee object
149156const bee = new Bee (" http://localhost:1633" )
157+
158+ // 2. Buy storage and get batch ID
150159const batchId = await bee .buyStorage (Size .fromGigabytes (1 ), Duration .fromDays (1 ))
160+ console .log (" Batch ID:" , String (batchId))
161+
162+ // 3. Create files for upload
163+ const files = [
164+ new File ([" <h1>Hello Swarm</h1>" ], " index.html" , { type: " text/html" }),
165+ new File ([" body{font-family:sans-serif}" ], " assets/main.css" , { type: " text/css" })
166+ ]
151167
152- // Using a directory picker: <input type="file" id="dir" webkitdirectory multiple>
153- const files = Array .from (document .querySelector (" #dir" )! .files ! )
168+ // 4. Upload multiple files (collection) → get collection reference
154169const res = await bee .uploadFiles (batchId, files)
155- console .log (" Collection reference :" , res .reference . toString ( ))
170+ console .log (" Collection ref :" , String ( res .reference ))
156171
157- // Download by relative path used at upload time:
158- const logo = await bee .downloadFile (res .reference , " images/logo.png" )
172+ // 5. Download files by relative path
173+ const page = await bee .downloadFile (res .reference , " index.html" )
174+ console .log (page .name ) // "index.html"
175+ console .log (page .data .toUtf8 ()) // prints file content
176+
177+ const style = await bee .downloadFile (res .reference , " assets/main.css" )
178+ console .log (style .name ) // "main.css"
179+ console .log (style .data .toUtf8 ()) // prints file content
159180```
160181
161182
@@ -177,14 +198,13 @@ const logo = await bee.downloadFile(res.reference, "images/logo.png")
1771984 . Download one file by its relative path
178199 ` const page = await bee.downloadFile(res.reference, "index.html") `
179200
180- 5 . Log the downloaded file’s metadata and contents
201+ 5 . Log the downloaded file name and contents
181202 ` console.log(page.name ?? "index.html") `
182- ` console.log(page.contentType) `
183203 ` console.log(page.data.toUtf8()) `
184204
185205** Full example:**
186206
187- ``` ts
207+ ``` js
188208import { Bee , Size , Duration } from " @ethersphere/bee-js"
189209
190210// 1) Connect to your Bee node HTTP API
@@ -193,17 +213,20 @@ const bee = new Bee("http://localhost:1633")
193213// 2) Buy storage (postage stamp batch)
194214const batchId = await bee .buyStorage (Size .fromGigabytes (1 ), Duration .fromDays (1 ))
195215
196- // 3) Upload all files under ./site (relative paths preserved); get a collection reference
197- const res = await bee .uploadFilesFromDirectory (batchId , " ./site " )
198- console .log (" Directory uploaded. Collection reference:" , res .reference .toString ())
216+ // 3) Upload all files under ./files (relative paths preserved); get reference
217+ const res = await bee .uploadFilesFromDirectory (batchId, " ./files " )
218+ console .log (" Directory uploaded. Collection reference:" , res .reference .toHex ())
199219
200- // 4) Download a specific file from the collection by its original relative path
201- const page = await bee .downloadFile (res .reference , " index.html" )
220+ // 4) Download files from the collection by original relative paths
221+ const page = await bee .downloadFile (res .reference , " root.txt" )
222+ const stylesheet = await bee .downloadFile (res .reference , " subdirectory/example.txt" )
223+
224+ // 5) Log the file name and contents to the terminal
225+ console .log (page .name ) // "root.txt"
226+ console .log (page .data .toUtf8 ()) // prints file content
202227
203- // 5) Log the file's metadata and contents to the terminal
204- console .log (page .name ?? " index.html" ) // "index.html"
205- console .log (page .contentType ) // e.g., "text/html"
206- console .log (page .data .toUtf8 ()) // Prints file content // prints file content
228+ console .log (stylesheet .name ) // "example.txt"
229+ console .log (stylesheet .data .toUtf8 ()) // prints file content
207230```
208231
209232> Tip: For ** binary** files, don’t convert to UTF-8 — log ` file.data.length ` or write to disk instead.
0 commit comments