获取整个文档,但 File.closeAsync 方法不起作用

问题描述 投票:0回答:1

我正在创建一个 Office 365Addin,它获取整个打开文档的数据。我使用 this 作为参考。

当我第一次和第二次单击“#submit”按钮时,一切正常,但之后就失败了。

据我所知,Document.getFileAsync只允许在内存中存储2个文档。 File.closeAsync 关闭内存上的文档以允许其他文档打开,在我的情况下,它不是返回 sliceCount 0 和大小 0。

File.closeAsync 是否存在错误,或者我是否在文档中遗漏了某些内容?

如有任何帮助,我们将不胜感激。

谢谢你,

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]);
}}
javascript office365 office-js microsoft-graph-api
1个回答
0
投票

你应该尝试一下这个。第一次收到响应时,您必须使用 file.closeAsync() 方法,然后每次需要时它都会工作

  Office.context.document.getFileAsync(Office.FileType.Compressed, { sliceSize: 65536 },
  function (result) {
      if (result.status === "succeeded") {
          const myFile = result.value;
          myFile.getSliceAsync(0, function (result) {

              let data = result.value.data

              let btoadata = btoa(String.fromCharCode.apply(null, new Uint8Array(data)))
              let fileobj = {

                  matterNumber: $scope.MatterNumber,
                  fileContent: btoadata
              }
              if (btoadata) {

                  myFile.closeAsync()
              }
              console.log(fileobj)
              ProgressLinearInActive();

          });

      } else {
          myFile.closeAsync()

          // Handle the error here
          loadToast("Upload Error");
          ProgressLinearInActive();

      }
  }

);

© www.soinside.com 2019 - 2024. All rights reserved.