Microsoft Graph API附件处理

时间:2019-12-18 23:35:18

标签: javascript microsoft-graph exchangewebservices azure-ad-graph-api microsoft-graph-mail

我似乎在从MS Graph API解码附件时遇到问题。至少,我认为这就是问题所在。下面是一个简单的JS示例。

        local.downloadAttachment = function (data) {
            $.get("https://graph.microsoft.com/v1.0/me/messages/" + self.EmailID() + "/attachments/" + data.id, null, function (results) {
                var bytes = new Uint8Array(results.contentBytes); // pass your byte response to this constructor
                var blob = new Blob([bytes], { type: results.contentType });// change resultByte to bytes

                //Alt method, also doesn't work but seems a lot closer to the right solution.
                //var altBytes = $.base64.decode(results.contentBytes);
                //var blob = new Blob([altBytes], { type: results.contentType });

                var link = document.createElement('a');
                link.href = window.URL.createObjectURL(blob);
                link.download = results.name;
                link.click();
            });
        };

我注意到几个奇怪的地方。 contentType似乎是错误的,PDF的类型为 application / octet-stream ,我曾期望 application / pdf 。 JPEG图像似乎输入正确,但是,我认为这不是问题。

我尝试在解码时使用atob,并使用/ $ results以二进制形式接收文件内容。

任何想法都很棒。

2 个答案:

答案 0 :(得分:0)

根据文档,Office 365 Unified API返回附件的**base64-encoded**内容。如果您对处理这些内容感兴趣,则首先需要将它们转换为原始二进制数据。从理论上讲,可以很容易地通过调用window.atob(base64string)函数来base64-decode内容字符串,然后将其转换为字节数组来实现。

不幸的是,如果在处理之后尝试打开文件,您会发现它已损坏并且其内容不可读。

事实证明,Office 365统一API返回的附件内容不是base64编码的,而是两次!因此,为了获取二进制内容,您必须将window.atob调用与另一个window.atob调用一起包装。这将使您能够按预期方式获取附件的二进制内容:

function getBinaryFileContents(base64FileContents) {
  // atob has to be called twice because the FileAttachment.ContentBytes property
  // return by the attachments endpoint is base64-encoded twice
  var raw = window.atob(window.atob(base64FileContents));
  var rawLength = raw.length;
  var array = new Uint8Array(new ArrayBuffer(rawLength));

  for(var i = 0; i < rawLength; i++) {
    array[i] = raw.charCodeAt(i);
  }

  return array;
}

其他参考:

https://blog.mastykarz.nl/office-365-unified-api-mail/

尝试看看是否有帮助。

答案 1 :(得分:0)

感谢@Mohit Verma,您帮助我重回正轨。但是,双重解码似乎并不是确切的问题,因为以下代码抛出

  

未捕获的DOMException:无法在“ Window”上执行“ atob”:   要解码的字符串未正确编码。

        local.downloadAttachment = function (data) {
            $.get("https://graph.microsoft.com/v1.0/me/messages/" + self.EmailID() + "/attachments/" + data.id, null, function (results) {
                var raw = window.atob(window.atob(results.contentBytes));
                var rawLength = raw.length;
                var array = new Uint8Array(new ArrayBuffer(rawLength));      // pass your byte response to this constructor

                for (var i = 0; i < rawLength; i++) {
                    array[i] = raw.charCodeAt(i);
                }

                var blob = new Blob([array], { type: results.contetType });

                var link = document.createElement('a');
                link.href = window.URL.createObjectURL(blob);
                link.download = results.name;
                link.click();
            });
        };

但是,由于将result.contentBytes转换为Blob构造函数期望的ByteArray,因此该代码有效。

        local.downloadAttachment = function (data) {
            $.get("https://graph.microsoft.com/v1.0/me/messages/" + self.EmailID() + "/attachments/" + data.id, null, function (results) {
                var raw = window.atob(results.contentBytes);
                var rawLength = raw.length;
                var array = new Uint8Array(new ArrayBuffer(rawLength));      // pass your byte response to this constructor

                for (var i = 0; i < rawLength; i++) {
                    array[i] = raw.charCodeAt(i);
                }

                var blob = new Blob([array], { type: results.contetType });

                var link = document.createElement('a');
                link.href = window.URL.createObjectURL(blob);
                link.download = results.name;
                link.click();
            });
        };