How to convert uploaded images to binary in ASP.NET Core File Upload

26 Aug 20261 minute to read

By default, the File Upload control saves the uploaded image files in physical directories. Additionally, you can convert the images into binary format on the server side before saving the uploaded images.

To retrieve the binary format of image files, convert the posted file’s input stream into a BinaryReader and read it as bytes using the ReadBytes method.

Refer to the following server-side code snippet.

[AcceptVerbs("Post")]
public IActionResult Save(IList<IFormFile> UploadFiles)
{
    IFormFile uploadedImage = UploadFiles.FirstOrDefault();
    if (uploadedImage.ContentType.ToLower().StartsWith("image/"))
    // Check whether the selected file is image
    {
        byte[] b;
        using (BinaryReader br = new BinaryReader(uploadedImage.OpenReadStream()))
        {
            b = br.ReadBytes((int)uploadedImage.OpenReadStream().Length);
            // Convert the image into bytes
        }
        Response.StatusCode = 200;
    }
    return Content("");
}

Explore the ASP.NET Core File Upload feature tour page to discover its groundbreaking features. You can also check out our ASP.NET Core File Upload example to see how to browse and select files for upload to the server.