Reputation: 12579
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.
Upvotes: 2
Views: 1045
Reputation: 700152
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.
Upvotes: 0
Reputation: 20562
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);
Upvotes: 7
Reputation: 1825
Convert.ToBase64String looks like your best option to store the bytes in a transmittable array, you should look into these functions.
Upvotes: 0
Reputation: 3845
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).
Upvotes: 0