1

我正在使用 vs2017 开发 Outlook javascript 插件。我创建了一个示例应用程序来从 Outlook 邮件项中查找附件。在这里,从 Exchange Server 获取附件时,它返回 200 OK。

我有自己的云应用程序,看起来像谷歌驱动器。我想使用 POST API 调用在我的云服务器上上传 Outlook 邮件附件。API 调用已成功运行。但我无法从交换服务器获取文件内容。

我在这里添加了一些示例代码。

  • 创建服务请求

            /// <reference path="../App.js" />
        var xhr;
        var serviceRequest;
    
        (function () {
            "use strict";
    
            // The Office initialize function must be run each time a new page is loaded
            Office.initialize = function (reason) {
                $(document).ready(function () {
                    app.initialize();
    
                    initApp();
                });
            };
    
            function initApp() {
                $("#footer").hide();
    
                if (Office.context.mailbox.item.attachments == undefined) {
                    var testButton = document.getElementById("testButton");
                    testButton.onclick = "";
                    showToast("Not supported", "Attachments are not supported by your Exchange server.");
                } else if (Office.context.mailbox.item.attachments.length == 0) {
                    var testButton = document.getElementById("testButton");
                    testButton.onclick = "";
                    showToast("No attachments", "There are no attachments on this item.");
                } else {
    
                    // Initalize a context object for the app.
                    //   Set the fields that are used on the request
                    //   object to default values.
                    serviceRequest = new Object();
                    serviceRequest.attachmentToken = "";
                    serviceRequest.ewsUrl = Office.context.mailbox.ewsUrl;
                    serviceRequest.attachments = new Array();
                }
            };
    
        })();
    
        function testAttachments() {
            Office.context.mailbox.getCallbackTokenAsync(attachmentTokenCallback);
        };
    
        function attachmentTokenCallback(asyncResult, userContext) {
            if (asyncResult.status == "succeeded") {
                serviceRequest.attachmentToken = asyncResult.value;
                makeServiceRequest();
            }
            else {
                showToast("Error", "Could not get callback token: " + asyncResult.error.message);
            }
        }
    
        function makeServiceRequest() {
            var attachment;
            xhr = new XMLHttpRequest();
            // Update the URL to point to your service location.
            xhr.open("POST", "https://localhost:8080/GetOutlookAttachments/AttachmentExampleService/api/AttachmentService", true);
    
            xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            xhr.onreadystatechange = requestReadyStateChange;
    
            // Translate the attachment details into a form easily understood by WCF.
            for (i = 0; i < Office.context.mailbox.item.attachments.length; i++) {
                attachment = Office.context.mailbox.item.attachments[i];
                attachment = attachment._data$p$0 || attachment.$0_0;
    
                if (attachment !== undefined) {
                    serviceRequest.attachments[i] = JSON.parse(JSON.stringify(attachment));
                }
            }
    
            // Send the request. The response is handled in the 
            // requestReadyStateChange function.
            xhr.send(JSON.stringify(serviceRequest));
        };
    
    
        // Handles the response from the JSON web service.
        function requestReadyStateChange() {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    var response = JSON.parse(xhr.responseText);
                    if (!response.isError) {
                        // The response indicates that the server recognized
                        // the client identity and processed the request.
                        // Show the response.
                        var names = "<h2>Attachments processed: " + response.attachmentsProcessed + "</h2>";
                        document.getElementById("names").innerHTML = names;
                    } else {
                        showToast("Runtime error", response.message);
                    }
                } else {
                    if (xhr.status == 404) {
                        showToast("Service not found", "The app server could not be found.");
                    } else {
                        showToast("Unknown error", "There was an unexpected error: " + xhr.status + " -- " + xhr.statusText);
                    }
                }
            }
        };
    
        // Shows the service response.
        function showResponse(response) {
            showToast("Service Response", "Attachments processed: " + response.attachmentsProcessed);
        }
    
        // Displays a message for 10 seconds.
        function showToast(title, message) {
    
            var notice = document.getElementById("notice");
            var output = document.getElementById('output');
    
            notice.innerHTML = title;
            output.innerHTML = message;
    
            $("#footer").show("slow");
    
            window.setTimeout(function () { $("#footer").hide("slow") }, 10000);
        };
    

请帮助我从 Outlook 邮件中获取附件并上传到我的云服务器上。

4

1 回答 1

0

attachment._data$p$0 提供附件元数据。从那里获取 id 并使用 getAttachmentContentAsync API 获取附件内容文档

于 2019-08-13T08:45:46.623 回答