【问题标题】:react-native-signature-capture Android - Unable to send image to api as formdatareact-native-signature-capture Android - 无法将图像作为表单数据发送到 api
【发布时间】:2021-02-23 20:38:50
【问题描述】:

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

我使用的是安卓 11

有人可以帮我吗?

【问题讨论】:

    标签: android react-native webapi


    【解决方案1】:

    这是代码示例

    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);
          });
    

    希望对您有所帮助。

    【讨论】:

    • 谢谢!另一个问题:我可以将 base64 作为多部分/表单数据发送吗?我从 android 读取文件时遇到问题(由于内部存储器的隐私限制),所以我想不保存签名文件而是使用 base64
    • 当签名库已经提供图像的 Uri 时,为什么要将文件保存到内部存储器。只需将该路径发送到您的 api。如果要使用base64,则必须使用RNFS库将图像解析为base64,然后将其发送到api。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    • 1970-01-01
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多