Im trying to download and save a file from a HttpWebResponse but im having problems saving the file (other than Text Files) properly.
I think its something to do with this part:
byte[] byteArray = Encoding.UTF8.GetBytes(http.Response.Content);
MemoryStream stream = new MemoryStream(byteArray);
Text Files work fine with the above code but when I try to save the Content to an Image file it gets corrupted.
How do i write this ‘string’ data to an image file (and other binary files)
Forgot to mention, This is .NET CP 3.5 and I have a wrapper class around the HttpWebResponse class to add OAuth etc.
The problem is you’re interpreting the binary data as text, even if it isn’t – as soon as you start treating the content as a string instead of bytes, you’re in trouble. You haven’t given the details of your wrapper class, but I’m assuming your
Contentproperty is returning a string – you won’t be able to use that. If your wrapper class doesn’t let you get at the raw data from the web response, you’ll need to modify it.If you’re using .NET 4, you can use the new CopyTo method:
If you’re not using .NET 4, you have to do the copying manually: