簡體   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