takeScreenShot() {
this.refs.viewShot.capture().then(uri => {
var details = {
'id': '1',
'name':'John Doe',
'image':uri };
//image uri = /private/var/mobile/Containers/Data/Application/FE…ctNative/249EB47D-DCEA-4C27-8174-0A7DCAA117CC.png
var formBody = [];
for (var property in details) {
var encodedKey = encodeURIComponent(property);
var encodedValue = encodeURIComponent(details[property]);
formBody.push(encodedKey + "=" + encodedValue);
}
formBody = formBody.join("&");
fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data',
},
body:formBody,
}).then((response) => response.json())
.then((responseJson) => {
console.log(responseJson)
}).catch(function (err) {
return console.log(err)
})
});
}
我正在尝试使用名称和ID将截取的屏幕快照图像上传到服务器,以上代码以uri作为字符串,如何将uri更改为可以上传到服务器目录的格式?谢谢
答案 0 :(得分:1)
RNFetchBlob.fetch('POST', url, {
Authorization : "Bearer access-token",
otherHeader : "foo",
'Content-Type' : 'multipart/form-data',
}, [
{ name : 'image', filename : 'avatar-foo.png', data: RNFetchBlob.wrap(uri)},
{ name : 'name', data : 'John Doe'},
{ name : 'id', data : '1'},
]).then((resp) => {
console.log(resp)
}).catch((err) => {
console.log(err)
})
有关更多信息,请参阅rn-fetch-blob