I want to offer docx files for download via browser and I've a problem with SaveToStream/SaveToFile as it is corrupting a (my) docx file while saving.
My system:
- Spire.Doc Pro Edition for .NET (Hot Fix) Version:4.1.20 (Licensed version)
- .NET 4.0
- VS2010
I have a docx template (see attached) which opens fine in Word 2007 and 2010. When I run through my code below the browser forwards the file correctly to Word, but Word refuses to open because of a "unkown error, Position: /word/header1.xml, row: 1, column: 0". Trying to save the download instead of opening showed the same error. The stored file refuses to open with the same error.
I also tried to use SaveToFile and afterwards send the saved file to download, which causes the same error.
It appears that the save function does some modifications which corrrupt the file...? If you look at my code you see that I just open the file and save it to stream without any modifications.
Here my code. Hope you could give me a hint.
- Code: Select all
Dim tmplDocPath As String = "D:\Bericht.docx"
Dim aStream As MemoryStream = New MemoryStream
Dim doc As New Document()
doc.LoadFromFile(tmplDocPath)
doc.SaveToStream(aStream, FileFormat.Docx)
Response.Clear()
Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
Response.AppendHeader("Content-Disposition:", "attachment; filename=Result.docx")
Response.AppendHeader("Content-Length", aStream.Length.ToString())
Response.BinaryWrite(aStream.ToArray())
Response.Flush()
Response.End()
Kind regards
Oliver