简体   繁体   English

无法将System.IO.Stream转换为Byte []

[英]Cannot convert System.IO.Stream to Byte[]

I have an image which is of type varbinary in db. 我有一个在db中类型为varbinary的图像。

In BO layer, it is declared as Byte[] . 在BO层中,它声明为Byte[]

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

To assign its value in BO layer, I used 为了在BO层中分配其值,我使用了

objBO.Image = FileUploadControl.FileContent

Its throws the error 它抛出错误

Connot convert System.IO.Stream to Byte[] 不能将System.IO.Stream转换为Byte []

Can anybody help in choosing the parameter of FileUploadControl that can be converted to Byte[] ? 有人可以帮助选择可以转换为Byte[]FileUploadControl参数吗?

Use FileUpload.FileBytes property to get Byte[] from the FileUploadControl 使用FileUpload.FileBytes属性从FileUploadControl获取Byte[]

Gets an array of the bytes in a file that is specified by using a FileUpload control. 获取通过使用FileUpload控件指定的文件中字节的数组。

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

The FileUpload control does not automatically read the file from the client. FileUpload控件不会自动从客户端读取文件。 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. 您编写的用于保存指定文件的代码可以调用FileBytes属性,该属性返回文件的内容。

instead of FileUploadControl.FileContent you must use FileUploadControl.FileBytes 而不是FileUploadControl.FileContent必须使用FileUploadControl.FileBytes

Filecontent gives you you the stream, so either convert the stream into bytes or directly use FileBytes Filecontent为您提供流,因此可以将流转换为字节或直接使用FileBytes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASPXRichedit无法将byte []隐式转换为System.IO.Stream - ASPXRichedit cannot implicity convert byte[] to System.IO.Stream 如何在System.IO.Stream中转换System.Byte []? - how to convert System.Byte[] in System.IO.Stream? 无法将类型'System.IO.Stream'隐式转换为'Stream' - Cannot implicitly convert type 'System.IO.Stream' to 'Stream' Vnext Argument 1:无法从'string'转换为'System.IO.Stream' - Vnext Argument 1: cannot convert from 'string' to 'System.IO.Stream' 参数1:无法从“ System.IO.Stream”转换为“ String” - Argument1: Cannot convert from 'System.IO.Stream' to 'String' 如何在 C# 中将 struct System.Byte byte[] 转换为 System.IO.Stream 对象? - How do I convert struct System.Byte byte[] to a System.IO.Stream object in C#? 如何将System.IO.Stream转换为图像? - How to convert System.IO.Stream into an Image? 无法将类型'System.IO.Stream'隐式转换为'System.IO.StreamReader - Cannot implicitly convert type 'System.IO.Stream' to 'System.IO.StreamReader 无法从“ System.IO.TextWriter”转换为“ System.IO.Stream” - cannot convert from 'System.IO.TextWriter' to 'System.IO.Stream' 无法将System.IO.Stream隐式转换为System.IO.MemoryStream。 C# - Cannot implicitly convert System.IO.Stream to System.IO.MemoryStream. c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM