而不是在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);
对的,这是可能的。您需要将返回类型更改为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 API或WCF(如果你需要SOAP支持)。
如果你想做BinaryWrite,你可能想要编写一个单独的IHttpHandler而不是web方法。 Web方法是围绕SOAP的东西,所以尽可能将它们入侵自定义响应,有点奇怪。