2

I want to output a Byte[] array to a string so I can send it along a HTTPRequest. Can it be done? And will the server pick up the data and create a file from it? Or does some special encoding need to be done?

The file is an image. At the moment I have:

Byte[] fBuff = File.ReadAllBytes("C:/pic.jpeg");

I need to take what's in fBuff and output it to send along a post request.

5 Answers 5

7

Use the Convert.ToBase64String method

Byte[] fBuff = File.ReadAllBytes("C:/pic.jpeg");
String base64 = Convert.ToBase64String(fBuff);

This way the string will as compact as posible and is sort of the "standard" way to writing bytes to string and back to bytes.

To convert back to bytes use Convert.FromBase64String:

String base64 = ""; // get the string
Byte[] fBuff = Convert.FromBase64String(base64);
Sign up to request clarification or add additional context in comments.

2 Comments

In Fiddler, this is what the string looks like. Would your method do the same? I'm not sure how the server decodes it, or what it expects. (I've only cut about 2% of the data from fiddler) ' .)10.)-,3:J>36F7,-@WAFLNRSR2>ZaZPJQRO��� �9,��������������������3�������!1"AQaq��#2B��3��������?��Ȉ�����������������������Y��}���3\���$ �����X�*��i)���xdQ����p�&����᮴r�z��T�!��A�B���������oچ�OE���g�1��q�V��MN�̻@8����ѫlW����׵��.��a w��3�S�2!W�K5$�d~���8�W~�9 �s�ꛡu-���5=־j�))��Ș$�w5���.��^�GO]t}v��Q�
argh! Didn't know it would mess up like that, sorry.
0

You could just create a String where each byte is a character of the String. If you do the same opposite procedure at the receiver you will not have any problems (I have done something similar but in Java).

2 Comments

This may not work well if the data has \r\n\r\n in the sequence and Content-Length is not specified in the POST.
Well for me it worked, but it was not exactly the same case (and it was in java). Maybe just better use Base64 as suggested (but remember the message is larger after the encoding).
0

Convert.ToBase64String looks like your best option to store the bytes in a transmittable array, you should look into these functions.

Comments

0

If you are sending just the file, you can use the UploadFile method of the WebClient class:

using (WebClient client = new WebClient) {
  client.UploadFile("http://site.com/ThePage.aspx", @"C:\pic.jpeg");
}

This will post the file as a regular file upload, just as from a web page with a file input. On the receiving server the file comes in the Request.Files collection.

Comments

0

Any reason of not using the WebClient upload file?

1 Comment

yes. I am sending the data as a multipart post request to a server I have no control over. I'm using HTTPRequest directly to send the data as WebClient has no such control over how data is sent.

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.