yarn add react-native-fs
async getWapData(userName) {
const toUrl = await uploadWap();
RNFS.uploadFiles({
// toUrl: 'http://localhost:4000/upload2',//文件上传路径
// toUrl: 'http://192.168.1.102:7898/file/uploadWap',//文件上传路径
toUrl: toUrl,//文件上传路径
files: [{
name: 'file',
filename: 'WAPDB',
filetype: 'application/octet-stream',
filepath: RNFS.LibraryDirectoryPath + '/LocalDatabase/WAPDB',
}], //上传的文件数组
fields: {
userName: userName,
userId: await AsyncStorage.getItem('userId'),
},
method: 'POST', // HTTP请求方法
begin: (res) => {
console.log('begin', res);
},
progress: (res) => {
let pr = ((res.totalBytesSent.toFixed(4) / res.totalBytesExpectedToSend.toFixed(4)).toFixed(4) * 100).toFixed(2) + '%';
global.loading.show('WAP' + pr, '', false);
},
}).promise.then((response) => { // HTTP response响应
if (response.statusCode == 200) {
global.loading.dismis();
// 删除数据库,
db.transaction((tx) => {
tx.executeSql('delete from wap', [], () => {
global.loading.dismis();
});
});
} else {
console.log('SERVER ERROR', response);
}
}).catch((err) => { //HTTP请求异常
if (err.description === 'cancelled') {
}
console.log(err);
});
}
// 这里最坑的地方就是那个name属性,其实是接口对应接收的name
网友评论