Binary file record

I am reading data from an iPhone application that uses http POST to send an image to a server. I can read this in a binary file and write to a file (see below). The problem is that I open the image does not work.

You can see the code from the iphone on this post: asp http post reading 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();

Image Content Type is Application / Octet Stream

Can someone shed some light on this, please.

+3
source share
2 answers

Is the next job in progress? To do this, it would be better to change FileStream to 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);

( , , , , ):

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

, Request.ContentLength ? , .: - (

, , :

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);
    }
}
+3

Source: https://habr.com/ru/post/1720182/


All Articles