|
7 | 7 | Store and serve files with Cloudflare R2. |
8 | 8 |
|
9 | 9 | ```ts |
| 10 | +// or @convex-dev/r2/svelte for Svelte! |
| 11 | +import { useUploadFile } from "@convex-dev/r2/react"; |
| 12 | + |
10 | 13 | // Upload files from React |
11 | 14 | const uploadFile = useUploadFile(api.example); |
12 | 15 | // ...in a callback |
@@ -90,8 +93,7 @@ npx convex env set R2_BUCKET xxxxx |
90 | 93 |
|
91 | 94 | ## Uploading files |
92 | 95 |
|
93 | | -File uploads to R2 typically use signed urls. The R2 component provides a React |
94 | | -hook that handles the entire upload processs: |
| 96 | +File uploads to R2 typically use signed urls. The R2 component provides hooks for React and Svelte that handle the entire upload process: |
95 | 97 |
|
96 | 98 | - generates the signed url |
97 | 99 | - uploads the file to R2 |
@@ -121,7 +123,9 @@ hook that handles the entire upload processs: |
121 | 123 | }); |
122 | 124 | ``` |
123 | 125 |
|
124 | | -2. Use the `useUploadFile` hook in a React component to upload files: |
| 126 | +2. Use the `useUploadFile` hook in your component to upload files: |
| 127 | + |
| 128 | + React: |
125 | 129 |
|
126 | 130 | ```tsx |
127 | 131 | // src/App.tsx |
@@ -165,6 +169,40 @@ hook that handles the entire upload processs: |
165 | 169 | } |
166 | 170 | ``` |
167 | 171 |
|
| 172 | + Svelte: |
| 173 | + |
| 174 | + ```svelte |
| 175 | + <script lang="ts"> |
| 176 | + import { useUploadFile } from "@convex-dev/r2/svelte"; |
| 177 | + import { api } from "../convex/_generated/api"; |
| 178 | +
|
| 179 | + const uploadFile = useUploadFile(api.example); |
| 180 | +
|
| 181 | + let selectedImage = $state<File | null>(null); |
| 182 | +
|
| 183 | + async function handleUpload(file: File) { |
| 184 | + await uploadFile(file); |
| 185 | + selectedImage = null; |
| 186 | + } |
| 187 | + </script> |
| 188 | +
|
| 189 | + <form |
| 190 | + onsubmit={() => { |
| 191 | + if (selectedImage) handleUpload(selectedImage); |
| 192 | + }} |
| 193 | + > |
| 194 | + <input |
| 195 | + type="file" |
| 196 | + accept="image/*" |
| 197 | + onchange={(e) => { |
| 198 | + selectedImage = e.currentTarget.files?.[0] ?? null; |
| 199 | + }} |
| 200 | + disabled={selectedImage !== null} |
| 201 | + /> |
| 202 | + <button type="submit" disabled={selectedImage === null}> Upload </button> |
| 203 | + </form> |
| 204 | + ``` |
| 205 | + |
168 | 206 | ### Using a custom object key |
169 | 207 |
|
170 | 208 | The `r2.generateUploadUrl` function generates a uuid to use as the object key by |
@@ -379,7 +417,7 @@ export const page = query({ |
379 | 417 | ### Accessing metadata after upload |
380 | 418 |
|
381 | 419 | The `onSyncMetadata` callback can be used to run a mutation after every metadata |
382 | | -sync. The `useUploadFile` React hook syncs metadata after every upload, so this |
| 420 | +sync. The `useUploadFile` hook syncs metadata after every upload, so this |
383 | 421 | function will run each time as well. |
384 | 422 |
|
385 | 423 | Because this runs after metadata sync, the `r2.getMetadata` can be used to |
|
0 commit comments