3

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: 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.

3
  • Are you asking what an application/octet-stream is or how to change it to a different MIME type? Commented Oct 15, 2009 at 13:44
  • Also, by "fails", waht do you mean? Commented Oct 15, 2009 at 13:47
  • when i have put failed I mean the image will not open, incorrect format. Opened it in Notpad and can see the header and content. Commented Oct 15, 2009 at 14:12

2 Answers 2

3

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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();

6 Comments

Can you gibe me an example how I would use the memory stream and how I would write the List<byte> data into this.
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.
That failed on this link: System.Drawing.Image image = System.Drawing.Image.FromStream(mem_stream); With Not a valid parameter
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?
you can see what data is being posted here: 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?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.