使用 getFileAsync(fileType, options, callback) 在本地保存 .docx 或 .ppt 文件
Saving a .docx or .ppt file locally using getFileAsync(fileType, options, callback)
我正在使用 Office Common API 创建一个加载项,我正在尝试在加载项中实现保存功能。本质上,这意味着我必须能够获取当前文档(.docx 或 .ppt)。我曾尝试使用 getFileAsync(fileType, options, callback)
并将 'filetype' 参数设置为 Office.FileType.Compressed
,但在下载文件时,我无法将其作为 .docx 或 .ppt 文件打开。我一直在使用以下代码:
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);
}
}
如何将 onGotAllSlices(docDataSlices)
中获得的结果解析为有效的 .docx 或 .ppt 文档?
我在发布问题后立即解决了问题。解决方法是创建一个新的 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);
}
我正在使用 Office Common API 创建一个加载项,我正在尝试在加载项中实现保存功能。本质上,这意味着我必须能够获取当前文档(.docx 或 .ppt)。我曾尝试使用 getFileAsync(fileType, options, callback)
并将 'filetype' 参数设置为 Office.FileType.Compressed
,但在下载文件时,我无法将其作为 .docx 或 .ppt 文件打开。我一直在使用以下代码:
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);
}
}
如何将 onGotAllSlices(docDataSlices)
中获得的结果解析为有效的 .docx 或 .ppt 文档?
我在发布问题后立即解决了问题。解决方法是创建一个新的 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);
}