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

时间:2018-12-14 05:19:02

标签: c# stream devexpress byte

我正在按照以下源代码使用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

和代码:

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;
    });
}

返回Encoding.UTF8.GetBytes(rtfText);

当所有联机文档都使用byte []在控件中打开文档时,我不断收到错误消息“无法将字节[]隐式转换为System.IO.Stream”。

可能会发生什么?为什么我的字节数组不被接受?

1 个答案:

答案 0 :(得分:0)

发生问题是因为ASPxRichEdit.Open()的第三个参数期望返回this reference中提供的System.IO.Stream类型的返回类型,但是您要通过GetBytes()传递字节数组:

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

要解决此问题,请尝试将字节数组转换为流,然后再返回,如下例所示:

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;
});

相关问题:

How do I convert struct System.Byte byte[] to a System.IO.Stream object in C#?