简体   繁体   中英

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

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

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.

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() :

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#?

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