'该流未打开用于写入。'异常问题

问题描述 投票:0回答:1

任务是更新存储在 SharePoint 上的

.docx
文件。因此,我使用 https://www.nuget.org/packages/DocumentFormat.OpenXml/ packege。

任何数据都应该存储在本地驱动器上,因此尝试使用

Stream
。 我有一个代码:

Stream docStream = await _httpClient!.GetStreamAsync(uri);

using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(docStream, true))
{
   string docText = null;
   using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
   {
        docText = sr.ReadToEnd();
   }

   Regex regexText = new Regex("OldText");
   docText = regexText.Replace(docText, "NewText");

   Stream outputDocStream = wordDoc.MainDocumentPart.GetStream(FileMode.Create);

   return outputDocStream;
}

代码第 2 行发生异常

DocumentFormat.OpenXml.Packaging.OpenXmlPackageException: 'The stream was not opened for writing.'

有人可以建议如何解决这个问题吗?

c# .net sharepoint httprequest
1个回答
0
投票

HttpClient
返回只读流,因此要对其中的数据进行任何更改,最简单的解决方案是将该流复制到
MemoryStream

© www.soinside.com 2019 - 2024. All rights reserved.