如何将 BASE64 转换为 URL?打字稿 - 角度 - 离子

时间:2021-01-25 21:22:36

标签: javascript angular typescript ionic-framework

有人知道如何将 BASE64 传递给 URL,我有一个应用程序 (Ionic) 可以生成 BASE64 并共享该图像,我需要该 URL。

我正在使用电容插件来分享图像:

import { Plugins } from '@capacitor/core';
const image = 'base64-string........'

async sharedLink(image: any) {
    await Plugins.Share.share({
      url: image
    });
}

当我从我的应用程序共享它时,只能看到 base64 字符串,我需要转换后的图像出来:

EXAMPLE 1EXAMPLE 2

3 个答案:

答案 0 :(得分:1)

您可以使用以下电容插件:https://ionicframework.com/docs/native/social-sharing

首先安装插件。

npm install cordova-plugin-x-socialsharing
npm install @ionic-native/social-sharing
ionic cap sync

app.module.ts

import { SocialSharing } from '@ionic-native/social-sharing/ngx';

providers: [
     SocialSharing
]

your-page.page.ts

import { SocialSharing } from '@ionic-native/social-sharing/ngx';

constructor(private socialSharing: SocialSharing)

const image = 'base64-string........'

async sharedLink(image: any) {
    this.socialSharing.share(null, null, image, null);
}

答案 1 :(得分:-1)

您可以分别使用 window.btoawindow.atob 函数编码/解码为 base64。非原语需要先序列化成JSON。

const plainData = { hello: "world" };         // Object { hello: "world" }
const jsonData = JSON.stringify(plainData);   // "{\"hello\":\"world\"}"
const base64Data = window.btoa(jsonData);     // "eyJoZWxsbyI6IndvcmxkIn0="
const decodedJson = window.atob(base64Data);  // "{\"hello\":\"world\"}"
const decodedParsedData = 
  JSON.parse("{\"hello\":\"world\"}");        // Object { hello: "world" }

答案 2 :(得分:-1)

每个浏览器都有一个encodeURI函数支持

var uri = "my test.asp?name=ståle&car=saab"; var res = encodeURI(uri);

但是查询字符串的大小是有限的,以这种方式传递图像不是一个好习惯,最好使用 POST 方法。