2010-11-08 54 views
1

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

服务片段推文档文件的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();

回答

1

这是通过web服务调用吗?如果您通过web服务代理客户端进行调用,那么它期待的是SOAP响应而不是二进制流。您将需要建模一个调用者可以理解的与SOAP兼容的响应。

设置content-type和content-disposition使浏览器可以做“正确”的事情(即打开文件保存对话框),但自定义客户端(httpwebclient,web服务代理等)没有这些行为内置的,所以你将需要添加任何遗漏。

+0

将我需要添加到SOAP头是什么? – 2010-11-08 16:05:33

+0

,因为除了这个响应问题之外,所有的工作都很顺利...... – 2010-11-08 16:08:01

0

你究竟在哪里写数据?

噢,试试这个...(如果你的.docx是形成良好的办公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(); 
+0

什么是数据字节? – 2010-11-08 16:23:33

+0

我在使用你的方法时得到这个错误客户端发现'application/msword'的响应内容类型,但是预计'text/xml'。 – 2010-11-08 16:26:10

+0

该文件已经制作完成,我只需选择该文件并将其附加到内容处置MIME标头 – 2010-11-08 16:27:14

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