Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Convert file to Byte array in c#

    • 0
    • 0
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 17.3k
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: