我遇到了 addFileAttachmentAsync 的问题。我有2个阵列: embeddedFiles (包含将附加到正文中的文件名)和 附件 (包含文件名,包含将是附件)。我为每个阵列运行了 循环,他们应该对数组中的每个文件向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
);
}
上面的代码采用查询字符串并调用 addFileAttachmentAsync 方法。 URL没有任何问题。我在浏览器上试过它们,然后根据自定义URL获取实际文件。 getAttachmentsContent 是一种只需调用 console.log(“blah”)的方法。
添加单个附件或内嵌图像时,它可以正常工作。但我需要添加多个附件和多个嵌入图像。以下是我尝试过的事情:
我得到的错误:
error OSF.DDA.Error code 9002 message "There was an internal format error." name : "InternalFormatError"*
对我而言,当您执行相同格式的多个请求时,它会中断。但我不确定为什么。
有什么想法吗?
答案 0 :(得分:0)
@Namig Ismayilov,addFileAttachmentAsync
方法是异步的。
因此,要调用多个异步调用,可以选择以下选项:
Promises
:( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)例如,对于您的情况,这是一种使用递归附加多个文件的方法。调用者应该只调用方法my_add_file_attachments_recursively()
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, "");
}