首页 > 解决方案 > 来自外部 url 的 Ionic 5 电容器/角度预览文件

问题描述

我尝试使用 previewanyfile cordova 插件从 Ionic 5 应用程序中的外部 url 打开文件。它适用于 android 但在 IOS 上我注意到有时它不会预览/打开 PDF 文件。只是一个带有文件名的灰色屏幕。但奇怪的是,一些 PDF 文件打开了。 文件预览画面

在此处输入图像描述

 previewProductDocument(url: string) {
    const loading = await this.loadingController.create({
      message: 'Loading document...',
    });
    loading.present().then(() => {
      this.previewAnyFile.preview(url).then((res) => {
        loading.dismiss();
      }).catch((err) => {
        loading.dismiss();
        this.presentToast('Error previewing the document try later', 'danger');
      });
    });
  }

这是我使用的插件 https://ionicframework.com/docs/native/preview-any-file

电容器版本“@capacitor/core”:“^2.2.0”,

仅在 IOS 模拟器 + Real IOS 设备上注意到此行为。知道这里发生了什么吗?

标签: iosangularionic-frameworkcapacitor

解决方案


链接中的特殊字符 (%2F)是问题的原因。

为了快速获胜;在处理之前更改链接或清理。

在这种情况下url.replace('%2F', '/')应该工作。

但是,另一个链接可能包含不同的字符。在没有 100% 确定的情况下,值得一试decodeURI,即decodeURI(url).


推荐阅读