【问题标题】: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,
    })
    

    【讨论】:

    • 不,这不起作用。还是一样
    • 你能告诉我你的要求和你的路线吗?
    猜你喜欢
    • 2023-03-26
    • 2021-12-20
    • 1970-01-01
    • 2018-01-20
    • 2021-02-06
    • 1970-01-01
    • 2017-12-19
    • 2022-01-28
    • 2020-05-20
    相关资源
    最近更新 更多