user2138814
user2138814

Reputation: 77

Cannot convert System.IO.Stream to Byte[]

I have an image which is of type varbinary in db.

In BO layer, it is declared as Byte[].

    public Byte[] Image
    {
         get { return p_image; }
         set { p_image = value; }
    }

To assign its value in BO layer, I used

objBO.Image = FileUploadControl.FileContent

Its throws the error

Connot convert System.IO.Stream to Byte[]

Can anybody help in choosing the parameter of FileUploadControl that can be converted to Byte[]?

Upvotes: 2

Views: 3629

Answers (2)

Qasim Javaid Khan
Qasim Javaid Khan

Reputation: 660

instead of FileUploadControl.FileContent you must use FileUploadControl.FileBytes

Filecontent gives you you the stream, so either convert the stream into bytes or directly use FileBytes

Upvotes: 1

Habib
Habib

Reputation: 223422

Use FileUpload.FileBytes property to get Byte[] from the FileUploadControl

Gets an array of the bytes in a file that is specified by using a FileUpload control.

FileUpload fileUpload = new FileUpload();
Byte[] fileBytes = fileUpload.FileBytes;

The FileUpload control does not automatically read the file from the client. You must explicitly provide a control or mechanism to allow the user to submit the specified file. For example, you can provide a button that the user can click to upload the file. The code that you write to save the specified file could call the FileBytes property, which returns the contents of the file.

Upvotes: 3

Related Questions