如何将.pptx文件转换为base64字符串?

时间:2019-11-05 10:56:26

标签: javascript angular typescript base64 office-addins

因此对于我正在从事的项目,我必须为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字符串硬编码到其中。 / p>

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

// 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()中的fileContent。现在有人可以解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

如果我对您的问题的理解正确,那么您想单击按钮时阅读一个文件,将其转换为base64字符串,然后将其传递给Powerpoint函数。这是您可以参考的普通Javascript解决方案;将代码转换成Typescript没什么大不了的。

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