简体   繁体   中英

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[] ?

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.

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

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