简体   繁体   中英

Image to binary returning System.byte[] instead of binary format in asp.net

I'm trying to convert image to binary and then store in database. I have a code to do this and after several Google searches, most answers are like the code I have written. The error I have is that instead of seeing a binary format in my database I'm getting System.byte[] as output. I also debugged and got the same thing. Here's part of the code

if (Upload.HasFile)
{
    HttpPostedFile postedFile = Upload.PostedFile;
    string filename = Path.GetFileName(postedFile.FileName);
    string fileExtension = Path.GetExtension(filename);
    int filesize = postedFile.ContentLength;
    if (fileExtension.ToLower() == ".jpg")
    {
        Stream stream = postedFile.InputStream;
        BinaryReader binaryreader = new BinaryReader(stream);

        byte[] bytes = binaryreader.ReadBytes((int)stream.Length);

        Debug.WriteLine(bytes);
    }
}

The result of my debug gives System.byte[] as output.

You can convert a byte array to string for DB storage

var storedString = Convert.ToBase64String(bytes);

and get the byte array from the stored string

bytes = Convert.FromBase64String(storedString);

If you really want to use the Binary format, you can look into the SoapHexBinary class, particularly the Parse() method and Value property

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM