Javascript 如何将.pptx文件转换为base64字符串?
所以对于我正在进行的一个项目,我必须为Office制作一个插件。在此加载项中,我需要从加载项打开其他pptx文件。我发现我需要将base64用于PowerPoint.createPresentation()函数。当我对其中的base64字符串进行硬编码时,它可以工作,但我需要从用户选择的文件生成字符串,该文件是从服务器中提取的 老实说,我没怎么试过。我对编程还是相当陌生的,并且在网上找到了一些东西。我确实在网上找到了一些东西,但我不知道如何实现它。我得到的大部分结果都是Javascript。我知道它应该在Typescript中工作,但正如我之前所说的,我不知道如何将其作为javascript代码或Typescript代码实现 目前的情况是,我从数组中获取Base64字符串。当用户选择一个选项时,将打开所选的pptx。该字符串当前已硬编码为 HTML: 我希望得到的结果是,当单击按钮时,函数将获取pptx文件,转换为base64,然后在新的powerpoint窗口中打开它,而不是硬编码base64字符串 编辑: 我尝试了一些我在某处找到的代码Javascript 如何将.pptx文件转换为base64字符串?,javascript,angular,typescript,base64,office-addins,Javascript,Angular,Typescript,Base64,Office Addins,所以对于我正在进行的一个项目,我必须为Office制作一个插件。在此加载项中,我需要从加载项打开其他pptx文件。我发现我需要将base64用于PowerPoint.createPresentation()函数。当我对其中的base64字符串进行硬编码时,它可以工作,但我需要从用户选择的文件生成字符串,该文件是从服务器中提取的 老实说,我没怎么试过。我对编程还是相当陌生的,并且在网上找到了一些东西。我确实在网上找到了一些东西,但我不知道如何实现它。我得到的大部分结果都是Javascript。我知
// 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函数。这里有一个你可以参考的普通Javascript解决方案;将代码转换成Typescript并不是什么大问题
谢谢您的回答,但不幸的是,我似乎不知道如何将其从读取用户输入改为读取本地存储的文件。
templates: any[] = [
{
src: 'base64string',
name: 'filename',
img: 'imgPath'
},
{
src: 'base64string',
name: 'filename',
img: 'imgPath'
},
];
async onCreateOpenPresentation(template) {
PowerPoint.createPresentation(template);
}
// 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);