小编Dha*_*gur的帖子

使用 FileInterceptor 将本机图像上传与 FormData 反应到 NestJS 服务器,但“文件”未定义

我的 React Native 应用程序使用该库接收选定的图像react-native-image-picker,我需要将该图像发送到运行 NestJS 服务器的后端。端点使用@UseInterceptorFileInterceptor从收到的 formData 的“文件”字段中提取图像。但是,当我向端点发出请求时,收到的文件未定义。

\n

这是我的 React Native 代码,发送请求并将 FormData 中的文件作为有效负载。

\n
const uploadNewProfileImage = async () => {\n    if (!newProfileImage?.assets) return;\n    const formData = new FormData();\n    const profileImage = newProfileImage.assets[0];\n    console.log(profileImage);\n    if (profileImage.uri && user) {\n      formData.append(\n        'file',\n        JSON.stringify({\n          uri:\n            Platform.OS === 'android'\n              ? profileImage.uri\n              : profileImage.uri.replace('file://', ''),\n          name: profileImage.fileName,\n          type: profileImage.type\n        })\n      );\n      client // client is an Axios instance that injects Bearer Token\n        .post(`/user/profile/${user.uid}/image`, formData)\n        .then(({ data …
Run Code Online (Sandbox Code Playgroud)

file-upload form-data multer react-native nestjs

5
推荐指数
1
解决办法
1350
查看次数

标签 统计

file-upload ×1

form-data ×1

multer ×1

nestjs ×1

react-native ×1