unstable_createFileUploadHandler
このコンテンツはまだ日本語訳がありません。
Node.js 上传处理程序会将带有文件名的部分写入磁盘,以免占用内存,没有文件名的部分将不会被解析。应与另一个上传处理程序组合使用。
例子:
export const action = async ({ request,}: ActionFunctionArgs) => { const uploadHandler = unstable_composeUploadHandlers( unstable_createFileUploadHandler({ maxPartSize: 5_000_000, file: ({ filename }) => filename, }), // parse everything else into memory unstable_createMemoryUploadHandler() ); const formData = await unstable_parseMultipartFormData( request, uploadHandler );
const file = formData.get("avatar");
// file is a "NodeOnDiskFile" which implements the "File" API // ... etc};选项:
| Property | Type | Default | Description |
|---|---|---|---|
| avoidFileConflicts | boolean | true | Avoid file conflicts by appending a timestamp on the end of the filename if it already exists on disk |
| directory | string | Function | os.tmpdir() | The directory to write the upload. |
| file | Function | () => upload_${random}.${ext} | The name of the file in the directory. Can be a relative path, the directory structure will be created if it does not exist. |
| maxPartSize | number | 3000000 | The maximum upload size allowed (in bytes). If the size is exceeded a MaxPartSizeExceededError will be thrown. |
| filter | Function | OPTIONAL | A function you can write to prevent a file upload from being saved based on filename, content type, or field name. Return false and the file will be ignored. |
file 和 directory 的函数 API 相同,接受一个 object 并返回一个 string,接受的对象有 filename、name 和 contentType(都是字符串),返回的 string 就是路径。
filter 函数接受一个 object 并返回一个 boolean(或解析为 boolean 的 promise)。它接受的对象具有 filename、name 和 contentType(均为字符串)。如果您要处理该文件流,则返回的 boolean 为 true。