Skip to content

Commit f79bf3d

Browse files
docs: remove old routing conventions from file uploads doc (#12323)
1 parent 3963006 commit f79bf3d

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

docs/how-to/file-uploads.md

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,33 @@
11
---
22
title: File Uploads
3-
# need to validate this guide and get rid of remixisms like file system routes
4-
hidden: true
53
---
64

75
# File Uploads
86

97
Handle file uploads in your React Router applications. This guide uses some packages from the [Remix The Web][remix-the-web] project to make file uploads easier.
108

11-
_Thank you to David Adams for [his original guide](https://programmingarehard.com/2024/09/06/remix-file-uploads-updated.html/) on how to implement file uploads in Remix. You can refer to it for even more examples._
9+
_Thank you to David Adams for [writing an original guide](https://programmingarehard.com/2024/09/06/remix-file-uploads-updated.html/) which this doc is based on. You can refer to it for even more examples._
1210

1311
## Basic File Upload
1412

13+
👉 **Setup some routes**
14+
15+
You can setup your routes however you like. This example uses the following structure:
16+
17+
```ts filename=routes.ts
18+
import {
19+
type RouteConfig,
20+
route,
21+
} from "@react-router/dev/routes";
22+
23+
export default [
24+
// ... other routes
25+
route("user/:id", "pages/user-profile.tsx", [
26+
route("avatar", "api/upload-avatar.tsx"),
27+
]),
28+
] satisfies RouteConfig;
29+
```
30+
1531
👉 **Add the form data parser**
1632

1733
`form-data-parser` is a wrapper around `request.formData()` that provides streaming support for handling file uploads.
@@ -32,7 +48,7 @@ You must set the form's `enctype` to `multipart/form-data` for file uploads to w
3248

3349
</docs-warning>
3450

35-
```tsx filename=routes/user.$id.tsx
51+
```tsx filename=pages/user-profile.tsx
3652
import {
3753
type FileUpload,
3854
parseFormData,
@@ -57,10 +73,10 @@ export async function action({
5773

5874
export default function Component() {
5975
return (
60-
<Form method="post" encType="multipart/form-data">
76+
<form method="post" encType="multipart/form-data">
6177
<input type="file" name="avatar" />
6278
<button>Submit</button>
63-
</Form>
79+
</form>
6480
);
6581
}
6682
```
@@ -97,7 +113,7 @@ export function getStorageKey(userId: string) {
97113

98114
Update the form's `action` to store files in the `fileStorage` instance.
99115

100-
```tsx filename=routes/user.$id.tsx
116+
```tsx filename=pages/user-profile.tsx
101117
import {
102118
FileUpload,
103119
parseFormData,
@@ -106,7 +122,7 @@ import {
106122
fileStorage,
107123
getStorageKey,
108124
} from "~/avatar-storage.server";
109-
import type { Route } from "./+types/user";
125+
import type { Route } from "./+types/user-profile";
110126

111127
export async function action({
112128
request,
@@ -165,11 +181,12 @@ export default function UserPage({
165181

166182
Create a [resource route][resource-route] that streams the file as a response.
167183

168-
```tsx filename=routes/user.$id.avatar.tsx
184+
```tsx filename=api/upload-avatar.tsx
169185
import {
170186
fileStorage,
171187
getStorageKey,
172188
} from "~/avatar-storage.server";
189+
import type { Route } from "./+types/upload-avatar";
173190

174191
export async function loader({ params }: Route.LoaderArgs) {
175192
const storageKey = getStorageKey(params.id);
@@ -194,4 +211,4 @@ export async function loader({ params }: Route.LoaderArgs) {
194211
[form-data-parser]: https://github.com/mjackson/remix-the-web/tree/main/packages/form-data-parser
195212
[file-storage]: https://github.com/mjackson/remix-the-web/tree/main/packages/file-storage
196213
[file]: https://developer.mozilla.org/en-US/docs/Web/API/File
197-
[resource-route]: ../how-to/resource-routes.md
214+
[resource-route]: ../how-to/resource-routes

0 commit comments

Comments
 (0)