Unable to download Docx file using MemoryStream

Hi Team,

Aspose.Words.Document doc = GetDocument(); // getting the document
MemoryStream docStream = new MemoryStream();
doc.Save(docStream, Aspose.Words.SaveFormat.Docx);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = “application/octet-stream”;
HttpContext.Current.Response.AddHeader(“content-disposition”, “attachment; filename=test.docx”);
byte[] bytes = docStream.GetBuffer();
HttpContext.Current.Response.BinaryWrite(bytes);
HttpContext.Current.Response.End();

Now after downloading and saving at location. And then after opening from that location it says the document is corrupted. Above code works fine with doc option.

I want directly file to be available for download.
I am using Aspose.words ver 15.3

Can anybody help me on this.

~
Sandeep

can anybody provide a solution to the problem.

Hi Sandeep,

Thanks for your inquiry. The MemoryStream.GetBuffer() method returns internal stream buffer and normally the size of this buffer is bigger than the actual stream length. Please try using the MemoryStream.ToArray() method instead. Also, please refer to the following article:

Best regards,

Hi,

I tried using MemoryStream.ToArray() but I am still getting the error. Could you please send the sample code for reference.

And I cannot downgrade the Aspose Dll.

Please help.

~
Sandeep Kumar

Hi Sandeep,

Thanks for your inquiry. You need to use the following overload of Document.Save method and code is simple as mentioned here:

Document.Save Method (HttpResponse, String, ContentDisposition, SaveOptions)

This overload of the Save method is not available when using the .NET Client Profile DLL. This DLL is located in the net3.5_ClientProfile folder. The .NET Client Profile excludes assemblies such as System.Web therefore HttpResponse is not available. This is entirely by design.

This can manifest itself as an error:

“No overload for method ‘Save’ takes ‘4’ parameters.”

If you need to use Aspose.Words in ASP.NET application, It is recommended to use the .NET 2.0 DLL where the correct overload described in this article is available.

Best regards,

Hi Hafeez,

I have done below changes and It worked for me.

Aspose.Words.Document doc = GetDocument(); // getting the document
MemoryStream docStream = new MemoryStream();
doc.Save(docStream, Aspose.Words.SaveFormat.Docx);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = “application/vnd.openxmlformats-officedocument.wordprocessingml.document”;
HttpContext.Current.Response.AddHeader(“content-disposition”,
“attachment;
filename=test.docx”);
byte[] bytes = docStream.ToArray());
HttpContext.Current.Response.BinaryWrite(bytes);
HttpContext.Current.Response.End();


Thanks for helping me out.

Hi Sandeep,


It is great you were able to find what you were looking for. Please let us know any time you have any further queries.

Best regards,