zproxy
zproxy

Reputation: 3549

How can i return raw bytes from ASP.NET web service?

If the WebMethod returns string it gets serialized to xml.

I want to return byte[] with responding ContentType - can I also specify it?.

Can it be done in ASP.NET web service web method?

Upvotes: 2

Views: 4467

Answers (3)

ChrisLively
ChrisLively

Reputation: 88064

Instead of a web service, use a Generic Handler (.ashx).

A Generic Handler accepts get/post requests and gives you the ability to completely control the output via the HttpContext.

I typically use these for sending files (pdfs, etc) to the browser.

Upvotes: 2

Guffa
Guffa

Reputation: 700322

You can return a byte array from a web service, but it will still be serialised into the response message. (Typically as base-64 in a SOAP XML response.)

If you want to return only the binary content you shouldn't use a web service. Instead you can use Response.BinaryWrite with a regular page with no html content, or context.BinaryWrite in a http handler.

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

ASMX web services use SOAP and the content type will always be application/soap+xml and the content represent xml. Even if you return a byte[] from your method this array will be base64 encoded into the soap body.

Upvotes: 2

Related Questions