unstable_createFileUploadHandler

unstable_createFileUploadHandler

此 API 已在 React Router v7 中移除。请参阅 the React Router guide to file uploads 了解推荐的替代 API。

一个 Node.js 上传处理程序,它会将带有文件名的部分写入磁盘,使其不占用内存,没有文件名的部分将不会被解析。应该与另一个上传处理程序组合。

¥A Node.js upload handler that will write parts with a filename to disk to keep them out of memory, parts without a filename will not be parsed. Should be composed with another upload handler.

示例:

¥Example:

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

选项:

¥Options:

属性 类型 默认 描述
avoidFileConflicts boolean true 如果文件名已存在于磁盘上,请在文件名末尾附加时间戳,以避免文件冲突。
directory 字符串 | 函数 os.tmpdir() 上传文件的目录。
file 函数 () => upload_${random}.${ext} 目录中文件的名称。可以是相对路径,如果目录结构不存在,则会创建目录结构。
maxPartSize number 3000000 允许的最大上传大小(以字节为单位)。如果超出大小,将抛出 MaxPartSizeExceededError。
filter 函数 OPTIONAL 一个你可以编写的函数,用于根据文件名、内容类型或字段名称阻止文件上传被保存。返回 false 时,该文件将被忽略。

filedirectory 的函数 API 相同。它们接受 object 参数并返回 string 参数。它接受的对象有 filenamenamecontentType(均为字符串)。返回的 string 是路径。

¥The function API for file and directory are the same. They accept an object and return a string. The object it accepts has filename, name, and contentType (all strings). The string returned is the path.

filter 函数接受一个 object 并返回一个 boolean(或一个解析为 boolean 的 Promise)。它接受的对象有 filenamenamecontentType(均为字符串)。如果你要处理该文件流,则返回的 booleantrue

¥The filter function accepts an object and returns a boolean (or a promise that resolves to a boolean). The object it accepts has the filename, name, and contentType (all strings). The boolean returned is true if you want to handle that file stream.

Remix v2.17 中文网 - 粤ICP备13048890号