tags:

views:

34

answers:

1

I want a user to choose an image to upload for his avatar.

So in my form, I'm asking his username, password, DOB, etc...

I'm making an object[] array and rounding up every field, and then pass that array to my method that saves the information to my Database. How can I "get" the binary information from the selected image (the DB field is type varbinary(max)) and save it as an object in the object[] array to THEN pass it to my Save() method?

+3  A: 

You should be able to pass a byte[] array into a varbinary(max) field. In which case:

void Submit_Click(object sender, EventArgs e)
{
    //read your other fields
    object file = ReadStream(myFileUploadControl.PostedFile.InputStream);
    //call save
}

public static byte[] ReadStream(Stream input)
{
    byte[] buffer = new byte[16 * 1024];
    using (MemoryStream ms = new MemoryStream())
    {
        int read;
        while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            ms.Write(buffer, 0, read);
        }
        return ms.ToArray();
    }
}
Rex M