コンテンツにスキップ

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
};

选项:

PropertyTypeDefaultDescription
avoidFileConflictsbooleantrueAvoid file conflicts by appending a timestamp on the end of the filename if it already exists on disk
directorystring | Functionos.tmpdir()The directory to write the upload.
fileFunction() => 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.
maxPartSizenumber3000000The maximum upload size allowed (in bytes). If the size is exceeded a MaxPartSizeExceededError will be thrown.
filterFunctionOPTIONALA 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。