无法使用wordpress rest-api和axios正确上传图像

时间:2019-09-06 19:05:19

标签: wordpress react-native axios wordpress-rest-api

我不知道如何正确地上传使用react-native-image-picker捕获的图像以使用axios和wordpress rest api进行wordpress。 我不了解如何使用表单数据和base64编码以及所有这些部分如何协同工作。图片/文件正在上传到Wordpress,但显示为灰色对象,表示未正确以正确的格式上传。非常感谢您的帮助。

捕获的图像具有以下对象

{
data:"....base64 encoding here...",
fileName:'IMG_20190906_000319.jpg',
fileSize:144446,
height:1280,
path:"/storage/emulated/0/DCIM/Camera/IMG_20190906_000319.jpg"
type:'image/jpeg',
uri:"content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F22/ORIGINAL/NONE/1498980218"
}

这是我发送发帖请求的方式

let url = weburl + `/wp/v2/media/`;
let imageCapture = image;
let formData = new FormData();
//I am sending the whole image object. I also tried to send image.data but all without success
formData.append('image',imageCaptured);

 axios.post( url,formData, {
      headers: {
        "Content-Disposition": `multipart/form-data; filename="city.jpg"`,
        'Content-Type': 'image/jpeg',
        "Authorization": 'Bearer ' + token
      }
    })

how the image in wordpress appears

1 个答案:

答案 0 :(得分:0)

我最终按照@ziyo的建议使用了rn-fetch-blob

  RNFetchBlob.fetch( 'POST',url, {
    'Content-Type' : 'application/octet',
     "Content-Disposition": `application/octet; filename="image.jpg`,
      "Cache-Control" : "no-store",
       "Authorization": 'Bearer ' + token
  },image.data
    ).then(function(data) {
      console.log(data)
    })