我正在从事Word AddIn项目。我正在尝试生成一个MmoryStream
来将该流保存到远程位置。基本上我应该做的是使用Globals.ThisAddIn.Application.ActiveDocument.WordOpenXML
来获取ActiveDocument的内容并将其保存在远程位置。
现在,我将this code将通过WordOpenXML
属性获得的整个字符串转换为MemoryStream。
在那之后,我将这个MemoryStream传递给HttpContent并为此编写了以下代码。
public async Task<HttpResponseMessage> PostDocumentAsync(SendAttachement sendAttachement)
{
if (sendAttachement == null)
{
throw new ArgumentNullException(nameof(sendAttachement));
}
using (var _client = new HttpClient(clientHandler, false))
{
_client.Timeout = new TimeSpan(0, 30, 0);
_client.BaseAddress = _URLParams.HostName;
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Add("UserNameOrEmail", Settings.Default.UserName);
_client.DefaultRequestHeaders.Add("Password", Settings.Default.Password);
_client.DefaultRequestHeaders.Add("User-Agent", "Word-Addins");
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
MultipartFormDataContent form = new MultipartFormDataContent();
foreach (var contentItem in sendAttachement.Contents)
{
HttpContent content = contentItem.streamContent;
content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
FileName = contentItem.FileName
};
form.Add(content);
}
var uri = _URLParams.APIPath + "/" + _URLParams.AbsolutePath + "?ClientId=" + sendAttachement.ClientId + "&RoomId=" + sendAttachement.RoomId + "&FolderId=" + sendAttachement.FolderId + "&ConflictStatus=" + sendAttachement.ConflictStatus;
return await _client.PostAsync(uri, form);
}
}
上面的代码给了我200 OK
,但是当我下载上传的文件时,却给了我Microsoft Word cannot open this document because some part of this file is missing is invalid.
现在我无法弄清楚我是在创建错误/损坏的流还是其他内容。
很抱歉这个愚蠢的问题,但是我已经尝试了尽可能多的解决方案。