【发布时间】:2016-09-26 05:26:29
【问题描述】:
我正在开发一个word插件,我需要将服务器上当前修改的文档作为.docx文件发送,我将创建一个api将当前文档上传到服务器(如果需要)。
请指导我,如何从 word 中获取当前文档并上传到服务器。
谢谢。
【问题讨论】:
标签: javascript ms-word xmlhttprequest ms-office office-js
我正在开发一个word插件,我需要将服务器上当前修改的文档作为.docx文件发送,我将创建一个api将当前文档上传到服务器(如果需要)。
请指导我,如何从 word 中获取当前文档并上传到服务器。
谢谢。
【问题讨论】:
标签: javascript ms-word xmlhttprequest ms-office office-js
你会想要使用Office.context.document.getFileAsync(...)。
请参阅http://dev.office.com/reference/add-ins/shared/document.getfileasync 了解更多信息。
从该页面复制其中一个示例:
function getDocumentAsCompressed() {
Office.context.document.getFileAsync(Office.FileType.Compressed, { sliceSize: 65536 /*64 KB*/ },
function (result) {
if (result.status == "succeeded") {
// If the getFileAsync call succeeded, then
// result.value will return a valid File Object.
var myFile = result.value;
var sliceCount = myFile.sliceCount;
var slicesReceived = 0, gotAllSlices = true, docdataSlices = [];
app.showNotification("File size:" + myFile.size + " #Slices: " + sliceCount);
// Get the file slices.
getSliceAsync(myFile, 0, sliceCount, gotAllSlices, docdataSlices, slicesReceived);
}
else {
app.showNotification("Error:", result.error.message);
}
});
}
function getSliceAsync(file, nextSlice, sliceCount, gotAllSlices, docdataSlices, slicesReceived) {
file.getSliceAsync(nextSlice, function (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();
onGotAllSlices(docdataSlices);
}
else {
getSliceAsync(file, ++nextSlice, sliceCount, gotAllSlices, docdataSlices, slicesReceived);
}
}
else {
gotAllSlices = false;
file.closeAsync();
app.showNotification("getSliceAsync Error:", sliceResult.error.message);
}
});
}
function onGotAllSlices(docdataSlices) {
var docdata = [];
for (var i = 0; i < docdataSlices.length; i++) {
docdata = docdata.concat(docdataSlices[i]);
}
var fileContent = new String();
for (var j = 0; j < docdata.length; j++) {
fileContent += String.fromCharCode(docdata[j]);
}
// Now all the file content is stored in 'fileContent' variable,
// you can do something with it, such as print, fax...
}
【讨论】: