【问题标题】:How do I convert a .pptx file to a base64 string?如何将 .pptx 文件转换为 base64 字符串?
【发布时间】:2019-11-05 10:56:26
【问题描述】:

因此,对于我正在进行的项目,我必须为 Office 制作一个插件。在这个插件中,我需要从插件中打开其他 pptx 文件。我发现我需要为 PowerPoint.createPresentation() 函数使用 base64。当我硬编码base64字符串时,它可以工作,但我需要从用户选择的文件中生成字符串,该文件是从服务器中提取的。

说实话,我没有尝试太多。我对编程还是很陌生,并且在网上找到了一些东西。我确实在网上找到了一些东西,但我不知道如何实现它。我得到的大部分结果都是使用 Javascript 的。我知道它应该在 Typescript 中工作,但正如我之前所说,我不知道如何将它实现为 javascript 代码或 typescript 代码。

目前的情况是我从一个数组中获取 Base64 字符串。当用户选择一个选项时,将打开所选的 pptx。该字符串当前是硬编码的。

HTML:

<div *ngFor="let template of templates">
  <button (click)="onClickOpenTemplate(template.src)">
    <img src="{{ template.img }}" alt="">
    <h3>{{ template.name }}</h3>
  </button>
</div>

打字稿:

templates: any[] = [
  {
    src: 'base64string',
    name: 'filename',
    img: 'imgPath'
  },
  {
    src: 'base64string',
    name: 'filename',
    img: 'imgPath'
  },
];

async onCreateOpenPresentation(template) {
    PowerPoint.createPresentation(template);
}

我想要的结果是,当单击按钮时,该函数将获取 pptx 文件,转换为 base64,然后在新的 powerpoint 窗口中打开它,而不是硬编码 base64 字符串。

编辑: 我尝试了一些我在某处找到的代码

// window.open(template);
    // tslint:disable-next-line: prefer-const
    let ActiveXObject: (type: string) => void;
    try {
      const fso = new ActiveXObject('Scripting.FileSystemObject');
      const file = fso.OpenTextFile(template, 1);
      const fileContent = file.ReadAll();
      file.Close();
      return fileContent;
      } catch (e) {
      if (e.number === -2146827859) {
        alert('Unable to access local files due to browser security settings. ' +
        'To overcome this, go to Tools->Internet Options->Security->Custom Level. ' +
        // tslint:disable-next-line: max-line-length
        'Find the setting for "Initialize and script ActiveX controls not marked as safe" and change it to "Enable" or "Prompt"');
      }
    }
    const templateBase64 = window.btoa(fileContent);
    console.log(templateBase64);
    PowerPoint.createPresentation(templateBase64);

但不幸的是,window.btao() 中的文件内容无法识别。有人现在可以解决这个问题吗?

【问题讨论】:

    标签: javascript angular typescript base64 office-addins


    【解决方案1】:

    如果我对您的问题的理解正确,您想在单击按钮时读取文件,将其转换为 base64 字符串并将其传递给 Powerpoint 函数。这是您可以参考的香草 Javascript 解决方案;将该代码转换为 Typescript 并不是什么大问题。

    Convert Input File to Base64 in Angular 2 without using FileReader In Ionic V2

    【讨论】:

    • 感谢您的回答,但不幸的是,我似乎无法弄清楚如何将其从读取用户输入更改为读取本地存储的文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    相关资源
    最近更新 更多