繁体   English   中英

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

[英]How do I convert a .pptx file to a base64 string?

因此,对于我正在进行的项目,我必须为 Office 制作一个插件。 在这个插件中,我需要从插件中打开其他 pptx 文件。 我发现我需要为 PowerPoint.createPresentation() function 使用 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>

Typescript:

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

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

The result what I would like to have is that when a button is clicked, the function will get the pptx file, convert is to base64 and then open it in a new powerpoint window instead of the base64 string being hardcoded in.

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

// 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() 中的文件内容。 有人现在我该如何解决这个问题?

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

在不使用 Ionic V2 中的 FileReader 的情况下,将输入文件转换为 Angular 2 中的 Base64

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM