none
Why file is corrupt? RRS feed

  • Question

  • Hello

    This is my code, adopted from this sample:

                            MemoryStream stream = new MemoryStream();

    using (WordprocessingDocument myDoc = WordprocessingDocument.Create( stream, WordprocessingDocumentType.Document,true)) { MainDocumentPart mainPart = myDoc.AddMainDocumentPart(); mainPart.Document= new Document(new Body(new Paragraph(new Run(new Text("Hello word"))))); mainPart.Document.Save(); myDoc.Save(); stream.Seek(0, SeekOrigin.Begin); using (FileStream file = new FileStream("D:\\iis access\\r.docx", FileMode.Create, System.IO.FileAccess.Write)) stream.CopyTo(file); b = stream.ToArray();

    }

    And the resulting file is corrupt. Im using nuget package 2.8.1. What can be wrong?

    Thanks



    • Edited by Alireza78 Wednesday, January 17, 2018 8:04 AM
    Tuesday, January 16, 2018 1:51 PM

Answers

  • For anyone who may need, this is the correct version. after saving mainpart, other lines must be out of using{} block.

                            MemoryStream stream = new MemoryStream();
                            using (WordprocessingDocument myDoc = WordprocessingDocument.Create( stream, WordprocessingDocumentType.Document,true))
                            {
                                MainDocumentPart mainPart = myDoc.AddMainDocumentPart();
                                mainPart.Document= new Document(new Body(new Paragraph(new Run(new Text("Hello word")))));
    
                                mainPart.Document.Save();
    
                            }
                                stream.Seek(0, SeekOrigin.Begin);
                                using (FileStream file = new FileStream("D:\\iis access\\r.docx", FileMode.Create, System.IO.FileAccess.Write))
                                    stream.CopyTo(file);
    
                                b = stream.ToArray();

    • Marked as answer by Alireza78 Wednesday, January 17, 2018 8:03 AM
    Wednesday, January 17, 2018 8:03 AM

All replies

  • For anyone who may need, this is the correct version. after saving mainpart, other lines must be out of using{} block.

                            MemoryStream stream = new MemoryStream();
                            using (WordprocessingDocument myDoc = WordprocessingDocument.Create( stream, WordprocessingDocumentType.Document,true))
                            {
                                MainDocumentPart mainPart = myDoc.AddMainDocumentPart();
                                mainPart.Document= new Document(new Body(new Paragraph(new Run(new Text("Hello word")))));
    
                                mainPart.Document.Save();
    
                            }
                                stream.Seek(0, SeekOrigin.Begin);
                                using (FileStream file = new FileStream("D:\\iis access\\r.docx", FileMode.Create, System.IO.FileAccess.Write))
                                    stream.CopyTo(file);
    
                                b = stream.ToArray();

    • Marked as answer by Alireza78 Wednesday, January 17, 2018 8:03 AM
    Wednesday, January 17, 2018 8:03 AM
  • Just wanted to add more information regarding this issue for future references.

    This problem occurred because WordprocessingDocument will memories the content that needs to be written to the stream, but it will not write it until either WordprocessingDocument.Close or WordprocessingDocument.Dispose is called.

    That is why it works after the using block has ended, because at that point the WordprocessingDocument.Dispose has been called.

    • Edited by Pusting Thursday, January 18, 2018 7:14 AM
    Thursday, January 18, 2018 7:13 AM