在使用react原生应用程序时,我在两个平台(iOS和Android(中都遇到了以下问题
环境:
"react-native": "^0.61.1",
"rn-fetch-blob": "^0.12.0"
我正在做一个多部分的请求,将视频上传到服务器。考虑到这个请求需要时间,我想向用户展示进度。
根据库文档,有一个uploadProgress
回调:https://github.com/joltup/rn-fetch-blob#uploaddownload-进步,但对我不利。
这是我的代码:
RNFetchBlob.fetch(
'POST',
`${REACT_APP_API_DOMAIN}/upload/video`,
{
Authorization: `Bearer ${token}`,
timestamp: `${timestamp}`,
'Content-Type': 'multipart/form-data',
},
[
{
name: 'video',
filename: `video.${file.split('.')[file.split('.').length - 1]}`,
data: RNFetchBlob.wrap(file),
},
],
)
.then(() => {
Notifier.showNotification({
title: 'The videos was successfully saved!',
description: "We're processing it, your profile will be updated soon!",
Component: NotifierComponents.Alert,
});
setStatus(statusEnum.PROCESSING);
completeCallback();
})
.uploadProgress((written, total) => {
console.log('uploaded', written / total);
})
.catch(err => {
Notifier.showNotification({
title: 'There was an error uploading the video',
description: err.toString(),
Component: NotifierComponents.Alert,
componentProps: {
alertType: 'error',
},
});
setStatus(statusEnum.FAILED);
completeCallback();
});
有什么想法吗?
有点晚了,但我认为你应该把uploadProgress
放在then
部分之前,所以它应该是:
RNFetchBlob.fetch(...).uploadProgress(...).then(...).catch(...)