Unable to open word file on client machine

I’m convert html to docx file in ASP.NET and try to open converted file in client machine. I’m using below code for this purpose but this doesn’t seems to work. No errors.

Document doc = new Document();
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);

        builder.MoveToDocumentEnd();

        StringBuilder finalHtml = new StringBuilder();
        finalHtml.Append("<html><head>");
        finalHtml.Append("<title></title></head><body>");
        finalHtml.Append(ucHtmlEditorBody.Html);
        finalHtml.Append("</body></html>");
        builder.InsertHtml(finalHtml.ToString());

        doc.CustomDocumentProperties.Add("PostBackURL", $@"{UpdateWordURL}");
        doc.CustomDocumentProperties.Add("CrsId", Convert.ToInt32(this.ObjectID.ToString()));
        

            doc.Save(Response, txtSubject.Text + ".docx", ContentDisposition.Attachment, null);

Any help in this regard will be highly appreciated.

@khurramshehzadd

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a sample application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

Dear @tahir.manzoor,

Word file is not generating, that is the problem.

@khurramshehzadd

Please ZIP and attach the requested resources here for testing. We will investigate the issue and provide you more information on it. Thanks for your cooperation.

@khurramshehzadd Have you tried using ContentDisposition.Inline? Actually saving document to the response internally the document is saved to a memory stream first and then copied to the response stream. So for testing purposes you can do the same - save your generated document to MemoryStream and then copy this stream to the response stream.

Dear @alexey.noskov,

I have tried using ContentDisposition.Inline, but no luck. Can you please share some samples for saving into memory stream ?

@khurramshehzadd You can use the following code for testing. it actually does the same as saving document to response.

MemoryStream memStream = new MemoryStream();
SaveOutputParameters result = doc.Save(memStream, SaveFormat.Docx);
response.ContentType = result.ContentType;
string headerValue = "attachment; filename*=UTF-8''%6F%75%74%2E%64%6F%63%78; filename=out.docx";
response.AddHeader("Content-Disposition", headerValue);
byte[] docData = memStream.ToArray();
response.OutputStream.Write(docData, 0, docData.Length);

`
@alexey.noskov, Thanks for the sample. It helps. My Scenario is, I want to open this word file automatically on client machine. is it possible ?

@khurramshehzadd In your case you should use Inline content disposition.

in this case the user will be prompted to save the file on the disk or open the document in the browser.