2010-11-08 111 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();