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
Copy file name to clipboardExpand all lines: packages/form-data-parser/README.md
+75-19Lines changed: 75 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,9 +12,17 @@ A streaming `multipart/form-data` parser that solves memory issues with file upl
12
12
13
13
## Why You Need This
14
14
15
-
The native [`request.formData()` method](https://developer.mozilla.org/en-US/docs/Web/API/Request/formData) has a major flaw in server environments: it buffers all file uploads in memory. When your users upload large files, this can quickly exhaust your server's RAM and crash your application.
15
+
The native [`request.formData()` method](https://developer.mozilla.org/en-US/docs/Web/API/Request/formData) has a few major flaws in server environments:
16
16
17
-
`form-data-parser` solves this by handling file uploads as they arrive in the request body stream, allowing the user to safely put the file in storage, and use some other value (like a unique identifier for that file) in the returned `FormData` object.
17
+
- It buffers all file uploads in memory
18
+
- It does not provide fine-grained control over file upload handling
19
+
- It does not prevent DoS attacks from malicious requests
20
+
21
+
In normal usage, this makes it difficult to process requests with large file uploads because they can exhaust your server's RAM and crash the application.
22
+
23
+
For attackers, this creates an attack vector where malicious actors can overwhelm your server's memory by sending large payloads with many files.
24
+
25
+
`form-data-parser` solves this by handling file uploads as they arrive in the request body stream, allowing you to safely store files and use either a) the `File` directly or b) a unique identifier for that file in the returned `FormData` object.
This library pairs really well with [the `file-storage` library](https://github.com/mjackson/remix-the-web/tree/main/packages/file-storage) for keeping files in various storage backends.
37
+
The `parseFormData` interface allows you to define an "upload handler" function for fine-grained control of handling file uploads.
// Is this file upload from the <input type="file" name="user-avatar"> field?
47
+
if (fileUpload.fieldName==='user-avatar') {
48
+
let filename =`/uploads/user-${user.id}-avatar.bin`;
49
+
50
+
// Store the file safely on disk
51
+
awaitfsp.writeFile(filename, fileUpload.bytes);
52
+
53
+
// Return the file name to use in the FormData object so we don't
54
+
// keep the file contents around in memory.
55
+
returnfilename;
56
+
}
57
+
58
+
// Ignore unrecognized fields
59
+
}
60
+
61
+
// Handle form submissions with file uploads
62
+
asyncfunction requestHandler(request:Request) {
63
+
// Parse the form data from the request.body stream, passing any files
64
+
// through your upload handler as they are parsed from the stream
65
+
let formData =awaitparseFormData(request, uploadHandler);
66
+
67
+
let avatarFilename =formData.get('user-avatar');
68
+
69
+
if (avatarFilename!=null) {
70
+
console.log(`User avatar uploaded to ${avatarFilename}`);
71
+
} else {
72
+
console.log(`No user avatar file was uploaded`);
73
+
}
74
+
}
75
+
```
76
+
77
+
To limit the maximum size of files that are uploaded, or the maximum number of files that may be uploaded in a single request, use the `maxFileSize` and `maxFiles` options.
If you're looking for a more flexible storage solution for `File` objects that are uploaded, this library pairs really well with [the `file-storage` library](https://github.com/mjackson/remix-the-web/tree/main/packages/file-storage) for keeping files in various storage backends.
0 commit comments