我正在处理需要将文件下载到移动设备中的自定义文件夹的应用。这是我使用的代码,
downloadImage(imageURL) {
file.createDir(this.storageDirectory, 'FolderName', false);
const fileTransfer: FileTransferObject = this.transfer.create();
const imageName = imageURL.split('/').pop();
fileTransfer.download(imageURL, this.storageDirectory + 'FolderName/' + imageName).then((entry) => {
const alertSuccess = this.alertCtrl.create({
title: `Download Succeeded!`,
subTitle: `${imageURL} was successfully downloaded to: ${entry.toURL()}`,
buttons: ['Ok']
});
alertSuccess.present();
}, (error) => {
console.log(error)
const alertFailure = this.alertCtrl.create({
title: `Download Failed!`,
subTitle: `${imageURL} was not successfully downloaded. Error code: ${error.code}`,
buttons: ['Ok']
});
alertFailure.present();
});
}
根据一些参考资料,我试图使用这样的承诺。
downloadImage(imageURL) {
var folderName = 'FolderName';
this.createDirectory(folderName);
console.log('After CreateDirectory');
this.platform.ready().then(() => {
const fileTransfer: FileTransferObject = this.transfer.create();
const imageName = imageURL.split('/').pop();
fileTransfer.download(imageURL, this.storageDirectory + 'FolderName/' + imageName).then((entry) => {
const alertSuccess = this.alertCtrl.create({
title: `Download Succeeded!`,
subTitle: `${imageURL} was successfully downloaded to: ${entry.toURL()}`,
buttons: ['Ok']
});
alertSuccess.present();
}, (error) => {
console.log(error)
const alertFailure = this.alertCtrl.create({
title: `Download Failed!`,
subTitle: `${imageURL} was not successfully downloaded. Error code: ${error.code}`,
buttons: ['Ok']
});
alertFailure.present();
});
});
}
createDirectory(folderName): Promise<Boolean> {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('createDirectory = ' + folderName)
}, 5000);
if(!this.platform.is('cordova')) {
return false;
}
if (this.platform.is('ios')) {
this.storageDirectory = this.file.externalRootDirectory;
}
else if(this.platform.is('android')) {
this.storageDirectory = this.file.externalRootDirectory;
}
else {
return false;
}
this.file.createDir(this.storageDirectory, folderName, false)
.then((success) => {
resolve(true);
})
.catch((error) => {
reject(false);
});
});
}
当我点击触发此功能的链接时,会显示一个弹出窗口,例如&#34;允许MyApp访问照片,媒体......&#34;,但在用户按下&#34;允许&#34之前;按钮,它显示错误弹出窗口。我尝试将其保留在异步块内,但我仍然面临同样的问题。
如何在用户确认弹出窗口之前推迟流程?对此有任何帮助将非常感激。