Android -无法将图像作为formdata发送到api



我有一个react应用程序,使用react-native-signature-capture组件(https://github.com/RepairShopr/react-native-signature-capture)来获取用户签名。经过多次尝试,我能够将签名文件保存在我的android设备的相机卷中。现在我应该将文件发送到需要formdata作为输入的webapi,但我找不到这样做的方法。

我正在使用android 11

有人能帮帮我吗?

下面是代码示例

const formdata = new FormData();
const signature = {
uri: result.pathName, // File path uri you get from signature library
type: 'image/jpeg', // image type
name: 'signature.jpg', // name of the image
};
formdata.append("signature", signature);

fetch('YOUR API URL', {
method: 'post',
headers: {
'Content-Type': 'multipart/form-data',
},
body: formdata,
})
.then((response) => {
console.log('image uploaded');
})
.catch((err) => {
console.log(err);
});

我希望这能帮助你。

最新更新