Expo FileSystem.uploadAsync 与多个文件

问题描述 投票:0回答:1

我收到了支持在我的 Expo React Native 应用程序中上传多个文件的新要求。我曾经使用 Expo 支持单个文件:

FileSystem.uploadAsync
,代码如下所示:

   const options = {
       mimeType: claimData.fileAttachment.mimeType,
       uploadType: FileSystemUploadType.MULTIPART,
       fieldName: "file",
       headers: {
          // Auth token and other headers
       },
       parameters: {
         // Extra fields
       },
     };

    return await uploadAsync(apiUrl, singleFileUrl, options);

仔细阅读 FileSystem API 的 Expo 文档后 我还没有找到任何关于如何处理多个文件的建议,以前有人遇到过这个吗?如果不对后端代码进行根本性的更改,我如何才能支持它。

react-native expo blob multipartform-data
1个回答
0
投票

建立要上传的文件列表,然后批量上传。

async function uplaodFile({ fileName, fileUrl }) {
  const options = {
    mimeType: claimData.fileAttachment.mimeType,
    uploadType: FileSystemUploadType.MULTIPART,
    fieldName: fileNmae,
    headers: {
      // Auth token and other headers
    },
    parameters: {
      // Extra fields
    },
  };

  return await uploadAsync(apiUrl, fileUrl, options);
}

async function uploadFiles() {
  const files = [
    {
      fileName: "File-01",
      fileUrl: "file://path/to/file1",
    },
    {
      fileName: "File-02",
      fileUrl: "file://path/to/file2",
    },
    {
      fileName: "File-03",
      fileUrl: "file://path/to/file3",
    },
  ];

  try {
    const uploadPromises = files.map(
      async (file) => await uploadFile({ ...file }),
    );

    const uploadResponses = await Promise.all(uploadPromises);
    //uploadResponses will contains list of file upload response (resolves & rejects)
  } catch (error) {
    console.error(error);
  }
}


© www.soinside.com 2019 - 2024. All rights reserved.