request downloadfile一台模拟器设备上测试失败,换一台真机是好的。换另外一台开发机上的IDE及模拟器也是好的。报错信息如下:
SendRequest 148: failed, handle:0 error:3 desc:*.ISystemAbilityManager
[request_manager.cpp LoadServerSuccess 408] load download server success
[request_manager.cpp OnRemoteDied 347] RequestSaDeathRecipient on remote systemAbility died.
代码如下:
getNetPic(context: Context, url: string,fileName: string,
success: (filePath : string) => void) {
hilog.error(0x0000, 'ch_test', 'context >>> ' + context.toString());
let filesDir = context.filesDir;
let temFileName = Date.now() + fileName
let temFile = filesDir + '/' + temFileName
hilog.error(0x0000, 'ch_test', 'temFile >>> ' + temFile);
try {
// 需要手动将 url 替换为真实服务器的 HTTP 协议地址
request.downloadFile(context, {
url: url,
filePath: temFile
}, (err: BusinessError, data: request.DownloadTask) => {
if (err) {
console.error(`Failed to request the download. Code: ${err.code}, message: ${err.message}`);
return;
}
let downloadTask: request.DownloadTask = data;
hilog.error(0x0000, 'ch_test', 'download complete >>> ' + downloadTask.toString());
downloadTask.on('complete', () => {
success(temFile)
})
});
} catch (err) {
console.error(`Failed to request the download. err: ${JSON.stringify(err)}`);
}
}
看样子是downloadTask on complete没有监听到。
如果downloadTask.on('complete')没有监听到,麻烦确认下失败的模拟设备上是否打开了网络权限。
也可以使用downloadTask.on('fail')监听下载失败的错误码,方便查找原因。