0

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?

1 Answer 1

3

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();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.