简体   繁体   中英

Asp.net Webservice returning docx file

I have some xml data. I've made an asp.net soap based webservice that takes the xml as a byte buffer applies xsl transformation to it and converts it to a docx file. But when i return the docx file using the response object i get an error that client found response of type application/vnd-word but it was expecting text/xml.

Service Snippet to push document file after xsl transformation

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

Is this via a webservice call? If you are calling via a webservice proxy client, then it is expecting a SOAP response and not a binary stream. You will need to model a SOAP-compatible response which the caller can understand.

Setting the content-type and content-disposition enables a browser to do the "right" thing (ie open the file-save dialog) but custom clients (httpwebclient, web service proxy, etc) don't have those behaviors built-in so you will need to add whatever is missing.

Where are you actually writing the data?

oh and try this... (if your .docx is a well formed office openxml file)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM