使用getFileAsync(fileType,options,callback)在本地保存.docx或.ppt文件

时间:2019-01-18 09:40:23

标签: ms-office powerpoint office-js docx

我正在使用Office的Common API创建一个加载项,并且试图在该加载项中实现保存功能。本质上,这意味着我必须能够获取当前文档(.docx或.ppt)。我尝试将$im = new Imagick('input.jpg'); $im->setImageFormat('jpeg'); $im->setGravity('Imagick::GRAVITY_CENTER'); $im->setImageBackgroundColor('white'); $im->extentImage( 480, 360); $im->writeImage('output.jpg'); 的'filetype'参数设置为getFileAsync(fileType, options, callback),但是在下载文件时,无法将其作为.docx或.ppt文件打开。我一直在使用以下代码:

Office.FileType.Compressed

如何将在public getDocumentAsCompressed() { Office.context.document.getFileAsync(Office.FileType.Compressed, { sliceSize: 65536 /*64 KB*/ }, result => { if (result.status.toString() === 'succeeded') { // If the getFileAsync call succeeded, then // result.value will return a valid File Object. const myFile = result.value; const sliceCount = myFile.sliceCount; const slicesReceived = 0, gotAllSlices = true, docdataSlices = []; console.log('File size:' + myFile.size + ' #Slices: ' + sliceCount); // Get the file slices. this.getSliceAsync(myFile, 0, sliceCount, gotAllSlices, docdataSlices, slicesReceived); } else { console.log('Error:', result.error.message); } }); } public getSliceAsync(file, nextSlice, sliceCount, gotAllSlices, docdataSlices, slicesReceived) { file.getSliceAsync(nextSlice, sliceResult => { if (sliceResult.status === 'succeeded') { if (!gotAllSlices) { // Failed to get all slices, no need to continue. return; } // Got one slice, store it in a temporary array. // (Or you can do something else, such as // send it to a third-party server.) docdataSlices[sliceResult.value.index] = sliceResult.value.data; if (++slicesReceived === sliceCount) { // All slices have been received. file.closeAsync(); this.onGotAllSlices(docdataSlices); } else { this.getSliceAsync(file, ++nextSlice, sliceCount, gotAllSlices, docdataSlices, slicesReceived); } } else { gotAllSlices = false; file.closeAsync(); console.log('getSliceAsync Error:', sliceResult.error.message); } }); } public onGotAllSlices(docdataSlices) { const blob = new Blob(docdataSlices); if (window.navigator.msSaveOrOpenBlob) { window.navigator.msSaveOrOpenBlob(blob, 'WordDownloadTest.docx'); } else { const a = document.createElement('a'), url = URL.createObjectURL(blob); a.href = url; a.download = 'WordDownloadTest.docx'; document.body.appendChild(a); a.click(); setTimeout(function() { document.body.removeChild(a); window.URL.revokeObjectURL(url); }, 0); } } 中获得的结果解析为有效的.docx或.ppt文档?

1 个答案:

答案 0 :(得分:1)

发布问题后,我设法解决了该问题。解决方法是创建一个新的UInt8Array,然后从该UInt8Array创建一个新的Blob。本质上,这意味着最后一种方法变为:

public onGotAllSlices(docdataSlices) {
    let docdata = [];
    for (let i = 0; i < docdataSlices.length; i++) {
      docdata = docdata.concat(docdataSlices[i]);
    }

    const byteArray = new Uint8Array(docdata);

    const blob = new Blob([byteArray]);

    const a = document.createElement('a');
    url = URL.createObjectURL(blob);
    a.href = url;
    a.download = 'WordTest.docx';
    document.body.appendChild(a);
    a.click();
    setTimeout(function() {
      document.body.removeChild(a);
      window.URL.revokeObjectURL(url);
    }, 0);
}