Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files from any .NET platform (C#, VB.NET, ASP.NET, .NET Core) and Java applications (J2SE and J2EE) with fast and high quality performance.

Fri Jul 01, 2011 2:50 am

Hi,
I have Word 2010 file on HDD.

I need download file as attachment from ASPX application and I cannot save doc to the file (hosting & permissions). Stream I am sending by this code:
Response.ContentType = "application/msword"
Response.AddHeader("Content-Disposition", "attachment;filename=Licence.docx")
Response.BinaryWrite(s.GetBuffer())

After I opened it (doc.LoadFromFile(filename, FileFormat.Docx)) I can save it to disc (doc.SaveToFile (filename2, FileFormat.Docx)) and everything is ok.
When I didn't save document to disc, but to the stream (doc.SaveToStream(stream, FileFormat.Docx)), document is corrupted

Do you have some ideas what can be wrong?

Martin

mzahumensky
 
Posts: 18
Joined: Fri May 20, 2011 11:29 am

Fri Jul 01, 2011 7:00 am

Dear Martin,
Thanks for your inquiry.
Your code have some problem. You can do it like this.
Code: Select all
 using (MemoryStream stream = new MemoryStream())
            {
                doc.SaveToStream(stream, FileFormat.Docx);
                Response.Clear();
                Response.ContentType = "application/msword";
                Response.AppendHeader("Content-Disposition:", "attachment; filename=Licence.docx");
                Response.AppendHeader("Content-Length", stream.Length.ToString());
                Response.ContentType = "application/download";
                Response.BinaryWrite(stream.ToArray());
                Response.Flush();
            }
Justin
Technical Support / Developer,
e-iceblue Support Team
User avatar

Justin Weng
 
Posts: 110
Joined: Mon Mar 28, 2011 5:54 am

Fri Jul 01, 2011 7:45 am

Dear Justin
Thank you very much for fast and perfect answer

My missing code is this
Response.AppendHeader("Content-Length", stream.Length.ToString());

Have a nice day!

Martin

mzahumensky
 
Posts: 18
Joined: Fri May 20, 2011 11:29 am

Return to Spire.Doc