简体   繁体   English

ASPXRichedit无法将byte []隐式转换为System.IO.Stream

[英]ASPXRichedit cannot implicity convert byte[] to System.IO.Stream

I am using the Devexpress control: aspxichedit following this source code: https://github.com/DevExpress-Examples/how-to-use-aspxrichedit-to-edit-rtf-data-in-aspxgridviews-editform-t260978/blob/15.1.5%2B/CS/Default.aspx.cs 我正在按照以下源代码使用Devexpress控件:aspxichedit: https : //github.com/DevExpress-Examples/how-to-use-aspxrichedit-to-edit-rtf-data-in-aspxgridviews-editform-t260978/blob /15.1.5%2B/CS/Default.aspx.cs

and at the code: 并在代码:

protected void re_Init(object sender, EventArgs e) {
    ASPxRichEdit richEdit = sender as ASPxRichEdit;
    GridViewEditItemTemplateContainer container = richEdit.NamingContainer as GridViewEditItemTemplateContainer;

    string documentID = GetDocumentID(container.Grid);
    if (!OpenedCanceledDocumentIDs.Contains(documentID)) {
        OpenedCanceledDocumentIDs.Add(documentID);
    }

    if (container.Grid.IsNewRowEditing) {
        richEdit.DocumentId = documentID;
        return;
    }

    //for text in db
    string rtfText = container.Grid.GetRowValues(container.VisibleIndex, "RtfContent").ToString();

    //for binary in db
    //byte[] rtfBinary = (byte[])container.Grid.GetRowValues(container.VisibleIndex, "RtfContent");

    richEdit.Open(documentID, DocumentFormat.Rtf, () => {
        //for text in db
        return Encoding.UTF8.GetBytes(rtfText);

        //for binary in db
        //return rtfBinary;
    });
}

At return Encoding.UTF8.GetBytes(rtfText); 返回时Encoding.UTF8.GetBytes(rtfText);

I kept getting the error 'Cannot implicity convert byte[] to System.IO.Stream' when all online documentations uses byte[] to open the document in the control. 当所有联机文档都使用byte []在控件中打开文档时,我不断收到错误消息“无法将byte []隐式转换为System.IO.Stream”。

What could be happening? 可能会发生什么? Why is my byte array not being accepted? 为什么我的字节数组不被接受?

The problem is occurred because the third parameter of ASPxRichEdit.Open() expects return type of System.IO.Stream type as provided in this reference , but you're passing byte array with GetBytes() : 发生问题是因为ASPxRichEdit.Open()的第三个参数期望此参考中提供的System.IO.Stream类型的返回类型,但是您要通过GetBytes()传递字节数组:

public void Open( 
   string documentId,  
   DocumentFormat format,  
   Func<Stream> contentAccessorByStream // => requires return type of stream
)

To fix this issue, try to convert that byte array to stream before returning it as in example below: 要解决此问题,请尝试将字节数组转换为流,然后再返回,如下例所示:

richEdit.Open(documentID, DocumentFormat.Rtf, () => {
    //for text in db
    var byteArray = Encoding.UTF8.GetBytes(rtfText);
    var stream = new MemoryStream(byteArray); // convert to stream

    return stream;
});

Related issue: 相关问题:

How do I convert struct System.Byte byte[] to a System.IO.Stream object in C#? 如何在C#中将结构System.Byte byte []转换为System.IO.Stream对象?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM