views:

80

answers:

2

I'm trying to send an image to a server, using HTTP Post Multipart. Everything else is fine, I have all the boundrys set and stuff.

But what do I have to do to the image before hand? Do I have to convert it to binary? Here is the header data from the header (using Fiddler). This is what I need to upload:

-----------------------------7daea2aa40c80
Content-Disposition: form-data; name="pict"; filename="pic.jpeg"
Content-Type: image/pjpeg

<Binary here ... or at least I think it is> ..
�����JFIF���������C� (lots more of this I removed)

Any advice?

+2  A: 

You can use the File.ReadAllBytes function to read the file into a byte[]. From there you can use a StreamWriter to output the bytes into your reponse. There is no conversion needed.

Michael Wulff Nielsen
Awesome stuff! Spent hours on the hunt for info related to this!
James Jeffery
A: 

Ideally you want to use a Content-Transfering-Encoding set to base64. Then you simply does File.ReadAllBytes if your file into a byte array, for then to use the Convert.ToBase64String method to converting to base64.

You can read more about it at Wikipedia's article about MIME

Example

string data =
       @"----------------------------7daea2aa40c80\n";
       + @"Content-Disposition: form-data; name="pict"; filename="{0}"\n"
       + @"Content-Type: image/pjpeg\n";
       + @"\n{1}";

string filename = "pict.jpg";
string bytes = Convert.ToBase64String(File.ReadAllBytes(filename));
string request = string.Format(data, filename, bytes);
Claus Jørgensen