将 react-native-signaturepad 输出转换为 formData



我需要将我的 react-native-signaturepad 输出作为图像上传到服务器。我已经尝试了以下代码

//this.state.base64 is signaturepad output
let  blob = new Blob([this.state.base64], {type: 'image/png'});
    alert(blob.size);
      var fd = new FormData()
      fd.append('file',blob,"Sign.png")
         let url="http://xxxxxx/xxxx/xxx/";
        fetch(url, {
          method: 'POST',
          headers: {        
            'Content-Type': 'multipart/form-data'
          },
          body: fd
        }).then(function (response) {
               alert("dfdfdfdfd")
        });

使用上面的代码我得到

"多部分主体必须至少有一个部分">

错误请任何人帮我解决这个问题

谢谢

我认为以

这种方式生成 blob 可能存在问题,因为 Blob(blobParts[, options]) ,支持的blobParts类型是

ArrayBufferArrayBufferViewBlobDOMString对象的Array,或任何此类对象的混合,将被放入 Blob 中。 DOMStrings编码为 UTF-8

因此,将

base64转换为 blob 的一种简单方法是使用 fetch API

let base64Url = // Your Base 64 URL
fetch(base64Url)
.then(res => res.blob())
.then(blob => //Use the blob here)

然后,您可以根据需要执行其余操作。

最新更新