Office365“;addFileAttachmentAsync”;执行多个请求时出错

Office365“;addFileAttachmentAsync”;执行多个请求时出错,office365,office-js,office365-apps,outlook-web-addins,Office365,Office Js,Office365 Apps,Outlook Web Addins,我遇到addFileAttachmentAsync问题。我有两个数组:嵌入文件(包含将附加到正文中的文件名)和附件(包含将附加到正文中的文件名)。我已经为每个数组运行了2个for循环,它们应该使用数组中的每个文件向Exchange Server发出GET请求,并取回二进制文件 for (var i = 0; i < embeddedFiles.length; i++) { var attachmentName =

我遇到addFileAttachmentAsync问题。我有两个数组:嵌入文件(包含将附加到正文中的文件名)和附件(包含将附加到正文中的文件名)。我已经为每个数组运行了2个for循环,它们应该使用数组中的每个文件向Exchange Server发出GET请求,并取回二进制文件

          for (var i = 0; i < embeddedFiles.length; i++) {
                            var attachmentName = (new Date()).getTime() + ".png";
                            var count = 0;
                            var options = { isInline: true, ContentId: attachmentName, asyncContext: { UniqueName: attachmentName } };
                            var attachmentURL = "http://" + document.location.hostname + document.location.port + '/MailForms/api/GetAttachment?' + 'AttId=' + embeddedFiles[i] + '&' + 'MwToken=' + token + '&' + 'ReqId=' + data.ReqId + '&' + 'userSmtp=' + smtp;
                            Office.context.mailbox.item.addFileAttachmentAsync(
                                attachmentURL,
                                attachmentName,
                                options,

                                function (asyncResult) {
                                    if (asyncResult.status == Office.AsyncResultStatus.Failed) {
                                        app.showNotification('Failed to add attachment', asyncResult.error.message);
                                    }
                                    else {

                                        var szCID = asyncResult.asyncContext.UniqueName;
                                        //var szAddBodyData = "<br><div><img height=150 width=150 src='cid:" + szCID + "'></div><br>"
                                        var bizimCigid = "cid:" + szCID;
                                        var index = "" + count;
                                        var oldsource = oBody.find('.mw-images')[index].attributes[1].value;
                                        oldsource = bizimCigid;
                                        //imagesource.replaceWith(bizimCigid);
                                        //Office.context.mailbox.item.body.setSelectedDataAsync(szAddBodyData, { coercionType: Office.CoercionType.Html });
                                        oBody.find('.mw-images')[index].attributes[1].value = oldsource;
                                        //Office.context.mailbox.item.body.setSelectedDataAsync({ coercionType: Office.CoercionType.Html });

                                        viewModel.updateComposeFormLast(subject, oBody["0"].innerHTML);
                                        count = count +  1;
                                    }


                                }


                            );

    for (var i = 0; i < attachments.length; i++) {

                            var attachmentURL = "http://" + document.location.hostname + document.location.port + '/MailForms/api/GetAttachment?' + 'AttId=' + attachments[i] + '&' + 'MwToken=' + token + '&' + 'ReqId=' + data.ReqId + '&' + 'userSmtp=' + smtp;

                            Office.context.mailbox.item.addFileAttachmentAsync(
                                attachmentURL,
                                attachments[i],
                                {
                                    'asyncContext': {}
                                },
                                viewModel.getAttachmentsContent
                                );
                        }
在我看来,当你对同一格式的多个请求执行时,它就会中断。但我不知道为什么


有什么想法吗?

@Namig Ismayilov,
addFileAttachmentAsync
方法是异步的

因此,要调用多个异步调用,以下是您的选项:

  • 使用承诺:()
  • 通过调用上一个异步方法的回调中的下一个异步方法来使用嵌套函数调用
  • 对嵌套调用使用递归。这只是上面第2点的延伸
  • 例如,对于您的情况,这里有一种使用递归附加多个文件的方法。调用方应该递归调用方法
    my\u add\u file\u attachments\u()

    函数my\u add\u file\u attachments\u递归\u helper(文件\u attachment\u arr、索引、消息)
    {
    if(索引<文件\附件\长度)
    {
    var file_attachment_obj=file_attachment_arr[索引];
    Office.context.mailbox.item.addFileAttachmentAsync
    (
    文件\附件\ obj.url,
    文件\附件\对象名称,
    {
    “异步上下文”:
    {
    “信息”:信息,
    “索引”:索引
    }
    },
    函数(异步结果)
    {
    var next_message=asyncResult.asyncContext.message+“id:”+asyncResult.value+“,“+”状态:“+asyncResult.status+”\n”;
    //在这里递归地添加下一个附件
    我的\u添加\u文件\u附件\u递归\u帮助程序(文件\u附件\u arr,asyncResult.asyncContext.index+1,下一条\u消息);
    }
    );
    }
    其他的
    {
    控制台日志(消息);
    }
    }
    函数my_add_file_attachments_递归地()
    {
    var文件\u附件\u arr=
    [
    {
    “名称”:“gold_puppy.jpg”,
    “url”:”https://foo/gold_puppy.jpg"
    },
    {
    “名称”:“black_puppy.jpg”,
    “url”:”https://foo/black_puppy.jpg"
    },
    {
    “名称”:“white_puppy.jpg”,
    “url”:”https://foo/white_puppy.jpg"
    }
    ];
    我的\u添加\u文件\u附件\u递归\u帮助程序(文件\u附件\u arr,0,“”);
    }
    
    欢迎使用堆栈溢出!既然你是新来的,我建议你阅读一些技巧。你的问题缺乏足够的细节来帮助社区帮助你。请在您的问题中直接包含您正在使用的代码和任何relivent错误消息。错误消息的屏幕截图不足以继续。
        error OSF.DDA.Error code 9002 message "There was an internal format error." name : "InternalFormatError"*
    
    function my_add_file_attachments_recursively_helper(file_attachment_arr, index, message)
    {
        if (index < file_attachment_arr.length)
        {
            var file_attachment_obj = file_attachment_arr[index];
    
            Office.context.mailbox.item.addFileAttachmentAsync
            (
                file_attachment_obj.url,
                file_attachment_obj.name,
                {
                    "asyncContext" :
                    {
                        "message" : message,
                        "index" : index
                    }
                },
                function (asyncResult)
                {
                    var next_message = asyncResult.asyncContext.message + "id : " + asyncResult.value + " , " + "status : " + asyncResult.status + "\n";
    
                    // add the next attachment here, recursively
                    my_add_file_attachments_recursively_helper(file_attachment_arr, asyncResult.asyncContext.index + 1, next_message);
                }
            );
        }
        else
        {
            console.log(message);
        }
    }
    
    function my_add_file_attachments_recursively()
    {
        var file_attachments_arr =
        [
            {
                "name" : "gold_puppy.jpg",
                "url" : "https://foo/gold_puppy.jpg"
            },
            {
                "name" : "black_puppy.jpg",
                "url" : "https://foo/black_puppy.jpg"
            },
            {
                "name" : "white_puppy.jpg",
                "url" : "https://foo/white_puppy.jpg"
            }
        ];
    
        my_add_file_attachments_recursively_helper(file_attachments_arr, 0, "");
    }