2015-05-05 55 views
1

我正在尝试用openxml创建一个word文档,并使用asp.net在IE中显示它。我有一个奇怪的行为。当我点击窗体上的一个按钮时,它假设从一个新窗口显示word文档。但是,它在主窗口底部显示一个错误,指出“filename.docx无法下载”。此时它会在错误旁边提供一个重试按钮。当我点击重试按钮,然后我提出了文件的文件!filename.docx无法使用打开xml下载

这里是带来了字的文件中的JavaScript:

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; 
    } 

这里是提交表单的代码:

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

下面是创建Word文档代码:

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(); 

       } 

      } 
     } 

我想知道为什么第一次失败?谢谢。

更新#1

我现在已经有IE,Chrome和Firefox的测试。这个问题是特定于IE的。我正在使用IE 9(9.0.8112.16421)。在Chrome和FireFox中,文档首次加载时没有错误。

更新#2

我已经提琴手检查原始数据:

这是第一篇文章,返回错误:

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 

这是第二个职位(后我点击重试按钮),它可以正确显示word文档:

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 
+0

尝试此的contentType:'应用/ vnd.openxmlformats-officedocument.spreadsheetml.sheet' – SmartDev

+0

SmartDev,感谢您的建议。我试了一下,它做了同样的事情:filename.docx无法下载。我点击重试然后出现。 –

回答

1

问题修复!为了服务于DOCX文件到IE 9,改变从下面的行:

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

到:

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