【问题标题】:base64 file to be downloaded in capacitor iOS and Androidbase64文件下载到电容iOS和Android
【发布时间】:2021-06-07 09:14:15
【问题描述】:

我有一个 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();
}

使用 document.getElementById 有问题吗?

【问题讨论】:

  • 你遇到了什么错误?
  • 我收到此错误:“无法打开 URL 数据:image/jpg;base64,iVBORw0KGgoAAAANSUhEUgAABzEAAANWCAYAAAB+vnHgAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADs ... rkJggg==: 错误域=NSOSStatusStatusErrorDomain 代码" UserInfo={_LSLine=229, _LSFunction=-[_LSDOpenClient openURL:options:completionHandler:]}"

标签: android ios angular capacitor


【解决方案1】:

从链接下载不适用于本机应用程序。为此,您需要使用Filesystem API, 例如:

import { FilesystemDirectory, Plugins } from '@capacitor/core';
const { Filesystem } = Plugins;

await Filesystem.writeFile({
    path: "filename.txt",
    data: "base64 data",
    directory: FilesystemDirectory.ExternalStorage,
  });

【讨论】:

    猜你喜欢
    • 2014-02-17
    • 2018-07-23
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 2022-11-10
    • 1970-01-01
    • 2013-07-07
    • 2021-01-26
    相关资源
    最近更新 更多