【问题标题】:How to Upload a local word docx file on server using javascript如何使用 javascript 在服务器上上传本地 word docx 文件
【发布时间】:2016-09-26 05:26:29
【问题描述】:

我正在开发一个word插件,我需要将服务器上当前修改的文档作为.docx文件发送,我将创建一个api将当前文档上传到服务器(如果需要)。

请指导我,如何从 word 中获取当前文档并上传到服务器。

谢谢。

【问题讨论】:

    标签: javascript ms-word xmlhttprequest ms-office office-js


    【解决方案1】:

    你会想要使用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...
    }
    

    【讨论】:

    • 感谢您的回复,由于我需要在服务器上发送文件,因此我已点击以下链接:请参阅dev.office.com/docs/add-ins/develop/… 我可以在 Slices 中获取文档内容并使用 api 发送,但是我不知道如何在服务器端获取切片并将每个单独的切片编译成单个 .docx 文件并存储在服务器上。目前 slices 以 base64 格式返回数据。请指导我获取服务器端的文档切片并创建一个 docx 文件。
    • 你应该让 JS 组装它(onGotAllSlices 就是这样做的)。然后将它作为一个大字符串发送到服务器,并使用 OpenXml SDK 之类的东西从那里处理文件。
    • @MichaelZlatkovsky-Microsoft 请您在Word Web Addin 上提供建议。完全卡住了。
    • @Asmi,我前段时间离开了 Office 可扩展性团队,所以恐怕没有什么可以帮上忙的。据我所知,StackOverflow 仍然是获得支持的正确地方,团队应该对其进行监控。如果您还没有收到回复,可以尝试再次提问(当然请注意,现在是假期,很多人都外出度假;所以一周左右您可能会有更好的运气)。祝你好运!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-21
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    • 2011-01-06
    • 1970-01-01
    相关资源
    最近更新 更多