Now since most of the applications are using APIs for data communication between server and client, the requirement of converting files to Byte array has increased immensely.
e.g. If we need to Upload a zip file to azure blob then we need to convert the zip file to byte array first and then pass it to the API who does the uploading of byte array to azure.
Code sample for the above mentioned requirement is as follows:
using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
{
// Create a byte array of file stream length
byte[] bytes = System.IO.File.ReadAllBytes(filename);
//Read block of bytes from stream into the byte array
fs.Read(bytes, 0, System.Convert.ToInt32(fs.Length));
//Close the File Stream
fs.Close();
return bytes; //return the byte data
}
Here fileName is the complete path of file with file name and extension of file.
0 Comment(s)