Robert Smith
Robert Smith

Reputation: 779

filename.docx couldn't be downloaded using open xml

I am attempting to create a word document with openxml and display it in IE using asp.net. I am having a strange behavior. When I click on a button in the form, it is suppose to display the word document from a new window. However, it displays an error in the bottom of the main window stating "filename.docx couldn't be downloaded". At this point it provides a retry button next to the error. When I click on the retry button, then i brings up the word file!

Here is the JavaScript that brings up the word file:

function openWindowWithPostRequest()
    {
        var winName = '_blank';
        var winURL = 'WordPublisher.aspx';
        var windowoption = 'resizable=yes,height=600,width=800,location=0,menubar=0,scrollbars=1';

        var form = document.createElement("form");
        form.setAttribute("method", "get");
        form.setAttribute("action", winURL);
        form.setAttribute("target", winName);

        document.body.appendChild(form);
        window.open('', winName, windowoption);
        form.target = winName;
        form.submit();
        document.body.removeChild(form);
        //window.close();

        return false;
    }

Here is the code that submits the form:

<input type="button" value="Create Word" onClick="openWindowWithPostRequest()" />

Here is the code that creates the word document:

protected void Page_Load(object sender, EventArgs e)
        {

            sendBackWordDocument();

        }


public void sendBackWordDocument()
        {
            using (MemoryStream mem = new MemoryStream())
            {
                // Create Document
                using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(mem,
          WordprocessingDocumentType.Document, true))
                {
                    // Add a main document part. 
                    MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();

                    new Document(new Body()).Save(mainPart);

                    Body body = mainPart.Document.Body;
                    body.Append(new Paragraph(
                                new Run(
                                    new Text("Hello World!"))));

                    mainPart.Document.Save();


                    wordDocument.Close(); // CLOSE DOCUMENT

                    // Stream it down to the browser

                    Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                    Response.AppendHeader("Content-Disposition", "attachment;filename=HelloWorld.docx");
                    mem.Position = 0;
                    mem.CopyTo(Response.OutputStream);
                    Response.Flush();
                    Response.End();

                }

            }
        }

I would like to know why the first time it fails? Thanks.

Update # 1

I have now tested with IE, Chrome and FireFox. The issue is specific to IE. I am using IE 9 (9.0.8112.16421). In Chrome and FireFox the document loads the first time with no errors.

Update # 2

I have checked the raw data in fiddler:

This is the first post that returns the error:

GET http://localhost:2290/WordPublisher.aspx HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Referer: http://localhost:2290/WordPublisher.aspx
Accept-Language: en-US
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Accept-Encoding: gzip, deflate
Host: localhost:2290
Connection: Keep-Alive

This is the second post (after I click on the retry button) that brings up the word document correctly:

GET http://localhost:2290/WordPublisher.aspx HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Host: localhost:2290
Connection: Keep-Alive

Upvotes: 1

Views: 210

Answers (1)

Robert Smith
Robert Smith

Reputation: 779

Problem fixed! In order to serve a docx file to IE 9, change the following line from:

Response.AppendHeader("Content-Disposition", "attachment;filename=HelloWorld.docx");

To:

Response.AppendHeader("Content-Disposition", "inline;filename=HelloWorld.docx");

Upvotes: 1

Related Questions