How do I save a byte[] returned from a WCF call?
In the code behind of my web page, I call a WCF service. The service returns a byte[] representing a file. In the code behind I have the following line that captures the bytes.
byte[] bytes = client.GeneratePDFBytes(crObj);
The question is, now what do I do with it? I wan开发者_运维百科t the user to be prompted to open/save the file. How do I add this functionality? I'm new to services and web stuff so it's likely something really simple but I'm overlooking it.
You can use the following:
Response.ContentType = "application/pdf";
Response.BinaryWrite(bytes);
Don't get hung up on the fact that you retrieved the byte array from a WCF service. Once you have the result stored in a byte array, you're looking at the exact same technique for writing to disk as if you read it out of a database or off the file system. If you change you research from WCF to flat-out reading/writing bytes I think you'll have better luck. Here's one from SO that had some good suggestions:
C# - How do I read and write a binary file?
Edit: I somehow managed to miss that this was ASP.NET... sorry about that, you have to write to response and set content type to binary then, as other answers have provided examples for.
精彩评论