tags:

views:

446

answers:

2
+2  Q: 

Write Binary File

I am reading data from an IPhone App that uses http POST to transfer the image to the Server I can read this into a an binary and it does write to a file (See below) the issue I have is when I open the image it fails.

You can see the code from the Iphone on this post: http://stackoverflow.com/questions/1547967/asp-http-post-read-data

Code:

byte[] buffer = new byte[Request.ContentLength];
using (BinaryReader br = new BinaryReader(Request.InputStream))
    br.Read(buffer, 0, buffer.Length);

string fileName = @"C:\test\test.jpg";
FileStream fs = new FileStream(fileName, FileMode.Create, 
    FileAccess.ReadWrite);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(buffer);
bw.Close();

The image Content-Type is application/octet-stream

Can anyone shine any light onto this please.

+2  A: 

Perhaps the Request.ContentLength was not set correctly? I know from bitter experience that it's not always safe to trust it. :-(

You can read the stream without knowing the length in advance like this:

const int bufferSize = 1024 * 64; // pick any reasonable buffer size
List<byte> buffer = new List<byte>();
using(Stream stream = new BufferedStream(request.InputStream, bufferSize)) {
    int value;
    while((value = stream.ReadByte()) != -1) {
        buffer.Add((byte) value);
    }
}
Christian Hayter
+1  A: 

Does the following work? For this it would probably be better to change the FileStream to a MemoryStream

fs.Position = 0;
Bitmap bit_map = Bitmap.FromStream(fs) as Bitmap;
bit_map.Save(@"C:\test\test.jpg");

Or

fs.Position = 0;
Image image = Image.FromStream(fs); 
image.Save(@"C:\test\test.jpg", ImageFormat.Jpeg);

Full Example (using your example, but if you want to change how the binary data is read from the other post, that is fine too, just confinue from after you have the buffer):

byte[] buffer = new byte[Request.ContentLength];
using (BinaryReader br = new BinaryReader(Request.InputStream))
    br.Read(buffer, 0, buffer.Length);
MemoryStream mem_stream = new MemoryStream (buffer); 
mem_stream.Write(buffer, 0, buffer.Length); 
mem_stream.Position = 0;
Image image = Image.FromStream(mem_stream); 
image.Save(@"C:\test\test.jpg", ImageFormat.Jpeg);
mem_stream.Close();
SwDevMan81
Can you gibe me an example how I would use the memory stream and how I would write the List<byte> data into this.
MartGriff
I updated the example using your code for reading the data into the buffer. You can change that to what Christain wrote if you want.
SwDevMan81
That failed on this link:System.Drawing.Image image = System.Drawing.Image.FromStream(mem_stream);With Not a valid parameter
MartGriff
I think this usually means there are some invalid characters in the buffer. How is the image encoded? Can you do a diff on the original image and the saved one to see the differences?
SwDevMan81
you can see what data is being posted here: http://stackoverflow.com/questions/1547967/asp-http-post-read-data Would it be due to the image information is bing sent back in the body and when writing to a file its putting all that information in to?
MartGriff
It was cause the image information was being sent in the body and I was trying to convert the whole body to an image.
MartGriff