简体   繁体   中英

Modifed word document not saving in memorystream

I am getting a word document from SharePoint using Microsoft graph API as a stream and changing some content in that file and downloading the saved content as a file but when I open the file, the modified content is not available. The downloaded file still shows the original content.

       using (var memoryStream = new MemoryStream())
         {
            templateStream.Position = 0;
            // Copying the stream that I've got into memory stream
            await templateStream.CopyToAsync(memoryStream).ConfigureAwait(false);
            memoryStream.Position = 0;

            using (var wordDocument = WordprocessingDocument.Open(memoryStream, true))
            {
                RevisionAccepter.AcceptRevisions(wordDocument);
                var document = wordDocument.MainDocumentPart.GetXDocument();
                var content = document.Descendants(W.p).ToList();
                //based on the dictionary I've I am replacing the contents of the file
                foreach (var field in dataDictionary)
                {
                    var regex = new Regex(field.Key, RegexOptions.IgnoreCase);
                    OpenXmlRegex.Replace(content, regex, field.Value.ToString(), null);
                }
                //not showing the modified content
                wordDocument.Save();
                //this is also not updating the memorystream variable with the modified content
                wordDocument.MainDocumentPart.Document.Save();
                memoryStream.Position = 0;
                await memoryStream.FlushAsync().ConfigureAwait(false);
            }

            var result = memoryStream.ToArray();
            memoryStream.Flush();
            return result;
        }

once I got the byte array from the above code I am downloading the file using this line from my controller

  return File(returnResponse, System.Net.Mime.MediaTypeNames.Application.Octet, $"Test- 
               {System.DateTime.Now}.docx");

What am I doing wrong?

在本概述答案你需要调用PutXDocument()的方法MainDocumentPart对于要成功地反映你的变化,因为目前你正在改变,但不是他们commiting到所需的文件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM