【问题标题】:How to send a file to backend in react-native using node.js如何使用 node.js 在 react-native 中将文件发送到后端
【发布时间】:2023-03-21 11:40:01
【问题描述】:
我试过了
const myData = uri; // this looks like file:///data/expo/...
const myDataResponse = await API.SendFile({
myData: myData
});
但是后端没有收到文件。有没有其他方法可以做到这一点?
在我得到的后端
console.log(req.body)
// I get a string same as uri of file
console.log(req.file)
// undefined
后端设置完美。使用邮递员时,我得到了成功的响应。
感谢任何帮助。我是一个初学者。
【问题讨论】:
标签:
node.js
react-native
expo
【解决方案1】:
你应该使用 FormData。
const data = new FormData();
// first argument is the key name received on you API
// second argument is your file path
data.append('file', filePath)
// then your request should look like this :
await fetch(url, {
method: 'post',
headers: {
'Content-Type': 'multipart/form-data',
},
body: data,
})