可以使用Response.BinaryWrite返回ASMX Web服务响应吗?

问题描述 投票:4回答:2

而不是在Web方法本身(作为SOAP XML)中返回二进制流(MTOM / Base64编码),例如:

[WebMethod]
public byte[] Download(string FileName)
....
return byteArray;

这种方法能以某种方式响应(可能通过Server对象)?:

Response.BinaryWrite(byteArray);

伪:

[WebMethod]
public DownloadBinaryWrite(string FileName)
...
Response.BinaryWrite(byteArray);
c# asp.net web-services webforms asmx
2个回答
8
投票

对的,这是可能的。您需要将返回类型更改为void,因为我们将直接写入响应,您需要手动设置内容类型并结束响应,以便它不会继续处理并发送更多数据。

[WebMethod]
public void Download(string FileName)
{
    HttpContext.Current.Response.ContentType = "image/png";
    HttpContext.Current.Response.BinaryWrite(imagebytes);
    HttpContext.Current.Response.End();
}

请注意,WebMethod现在是not really supported,你应该切换到Web APIWCF(如果你需要SOAP支持)。


0
投票

如果你想做BinaryWrite,你可能想要编写一个单独的IHttpHandler而不是web方法。 Web方法是围绕SOAP的东西,所以尽可能将它们入侵自定义响应,有点奇怪。

© www.soinside.com 2019 - 2024. All rights reserved.