我有一个应用程序在angular和我包装的应用程序与电容器在iOS和Android。该应用程序包含附件,我从后端请求,结果是base64字符串。我设法在网页上下载文件没有问题,但文件无法在移动版本下载。这是下载代码。
<div *ngFor="let attachment of attachmentsInput, let i = index">
<div class="row pl-1 pr-1">
<div class="col-lg-7">
<a role="button" (click)="downloadFile(i)" class="hover">
<span class="hover">{{attachment.name}}</span>
</a>
<a style="display: none;" id="downloadLinkId+{{i}}" [href]="attachmentsInput[i].toBeDownloaded" [download]="attachmentsInput[i].name"></a>
</div>
</div>
</div>
下载文件代码:
async downloadFile(index) {
this.attachmentsInput[index].toBeDownloaded = this.sanitizer.bypassSecurityTrustResourceUrl('data:image/jpg;base64,' + this.attachmentsInput[index].content);
if (this.attachmentsInput[index].toBeDownloaded)
document.getElementById('downloadLinkId+' + index).click();
}
使用文档有问题吗?getElementById ?
在本机应用程序中无法从链接下载。为此,您需要使用Filesystem API,例如:
import { FilesystemDirectory, Plugins } from '@capacitor/core';
const { Filesystem } = Plugins;
await Filesystem.writeFile({
path: "filename.txt",
data: "base64 data",
directory: FilesystemDirectory.ExternalStorage,
});