【问题标题】:Asp.net Webservice returning docx fileAsp.net Webservice 返回 docx 文件
【发布时间】:2011-05-06 17:21:29
【问题描述】:

我有一些 xml 数据。我制作了一个基于 asp.net soap 的 web 服务,它将 xml 作为字节缓冲区应用 xsl 转换并将其转换为 docx 文件。但是,当我使用响应对象返回 docx 文件时,我收到一个错误,客户端找到类型为 application/vnd-word 的响应,但它期待的是 text/xml。

xsl 转换后推送文档文件的服务片段

Context.Response.Clear();

Context.Response.ClearHeaders();

Context.Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";

Context.Response.AddHeader("Content-Disposition", "attachment; filename=" + "test.docx"); Context.Response.Flush();

Context.Response.End();

【问题讨论】:

    标签: asp.net web-services docx


    【解决方案1】:

    这是通过网络服务调用吗?如果您通过 Web 服务代理客户端调用,那么它期望的是 SOAP 响应而不是二进制流。您需要为调用者可以理解的 SOAP 兼容响应建模。

    设置 content-type 和 content-disposition 使浏览器能够做“正确”的事情(即打开文件保存对话框),但自定义客户端(httpwebclient、Web 服务代理等)没有构建这些行为-in 所以你需要添加任何缺少的东西。

    【讨论】:

    • 我需要在肥皂标题中添加什么?
    【解决方案2】:

    您实际上在哪里写入数据?

    哦,试试这个...(如果您的 .docx 是格式良好的 office openxml 文件)

        Response.Clear();
        Response.ContentType = "application/msword";
        Response.AppendHeader("Content-Type", "application/msword");
        Response.AppendHeader("Content-disposition", String.Format("attachment; filename={0}", FileName));
        Response.BinaryWrite(DataBytes);
        Response.End();
    
        Response.Flush();
    

    【讨论】:

    • 我在使用您的方法时收到此错误客户端发现响应内容类型为“application/msword”,但预期为“text/xml”。
    • 文件已经制作好了,我只需选择文件并将其附加到内容处置 MIME 标头
    • 哦,那就尝试更改内容类型和标题。 Databytes 是包含 .docx 数据的字节数组。一秒钟..我会发布示例。
    • 当然,请发布一个我尝试过不同内容类型的示例。
    • Context.Response.Clear(); Context.Response.ContentType = "应用程序/msword"; Context.Response.AddHeader("Content-Type", "application/msword"); Context.Response.AddHeader("Content-Disposition", "attachment; filename=" + "test.docx"); Context.Response.End(); Context.Response.Flush();
    【解决方案3】:
    Context.Response.Clear();
    Context.Response.ContentType = "application/msword"; 
    Context.Response.AddHeader("Content-Type", "application/msword"); 
    Context.Response.AddHeader("Content-Disposition", "attachment; filename=" + "test.docx"); 
    Context.Response.End();
    Context.Response.Flush();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-04
      • 1970-01-01
      • 1970-01-01
      • 2019-04-17
      • 2014-04-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-15
      相关资源
      最近更新 更多