【发布时间】:2019-02-24 08:24:27
【问题描述】:
我有一个demo-file.csv 文件,它在assets/csv 文件夹中,所以我如何从手机下载它,
这是我的 HTML 和组件代码。
HTML 代码
<button ion-button type="button" block (click)="downloadFile('assets/csv/demo-file.csv', 'demo-file.csv')">Download Demo File</button>
组件代码
public downloadFile(link: any, fileName: any) {
if (link) {
let path = null;
this.showWaitingLoading();
if (this.platform.is('ios')) {
path = this.file.documentsDirectory;
} else {
path = this.file.dataDirectory;
}
const transfer = this.transfer.create();
transfer.download(link, path + fileName).then(entry => {
this.dismissWaitingLoading();
this.openFile(entry.toURL());
}).catch(() => {
this.dismissWaitingLoading();
this.showToastMsg('error', "Something went wrong");
});
}
}
/* ================= OPNE FILE FUNCTION ===========*/
public openFile(path: any) {
this.fileOpener.open(path, 'application/*')
.then(() => console.log('File is opened'))
.catch((e: any) => console.log('Error openening file', e));
}
我无法下载文件,我的 PATH 中是否缺少任何东西?
【问题讨论】:
-
你不需要前导斜线吗?
/assets/csv/demo-file.csv?下载失败时会出现什么错误? -
@David 我也尝试使用斜线,但没有成功。
-
那么你得到了什么错误?
标签: javascript angular ionic2 ionic3 file-transfer