Description : In this post how to convert a file stream in a byte array using c#. this code is use for convert a file into byte array and also send this byte array into FTP or Other file storage code. in my early post of upload file in FTP using c# use this code for convert any file from stream or file path to byte array
- Below code for convert file stream to byte array from specific file path using c#
public byte[] ReadAllBytes(string fileName)
{
byte[] buffer = null;
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
buffer = new byte[fs.Length];
fs.Read(buffer, 0, (int)fs.Length);
or
using (var reader = new BinaryReader(filestream))
{
byte[] file = reader.ReadBytes((int)filestream.Length);
}
}
return buffer;
}
- Below code for convert file stream to byte array from specific file path using c#
public byte[] ReadAllBytes(string fileName)
{
byte[] buffer = null;
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
buffer = new byte[fs.Length];
fs.Read(buffer, 0, (int)fs.Length);
or
using (var reader = new BinaryReader(filestream))
{
byte[] file = reader.ReadBytes((int)filestream.Length);
}
}
return buffer;
}