importing Byte array using DocxFormatProvider

2 posts, 0 answers
  1. jaimin
    jaimin avatar
    8 posts
    Member since:
    Oct 2010

    Posted 03 Nov 2010 Link to this post

    i am using latest version of RadControls in which you provided extended API for importing/exporting from/to byte array for binary formats.

     I am using DocxFormatProvider.
     use export API to save Byte Array in database.
     and import API to load the Byte Array.

    Following is the property i am using in View Model



    public RadDocument ParDocument












    RadDocument doc = new RadDocument();





    if(Particular != null && Particular.wp_perticulars != null)



    doc = provider.Import(Particular.wp_perticulars);





    return doc;












    MemoryStream output = new MemoryStream();







    Particular.wp_perticulars = output.GetBuffer();




    in which wp_perticulars is image DataType in database.
    The problem is it exporting fine and saving byte array in database.
    when i am importing the byte array. its give me error "Invalid data".

    Error During import:
    {System.InvalidOperationException: Invalid data.
       at Telerik.Windows.Documents.Packaging.ZipPackage.Open(Stream stream, FileAccess access)
       at Telerik.Windows.Documents.FormatProviders.OpenXml.Docx.Import.DocxImporter..ctor(Stream input)
       at Telerik.Windows.Documents.FormatProviders.OpenXml.Docx.DocxFormatProvider.Import(Stream input)
       at Telerik.Windows.Documents.FormatProviders.DocumentFormatProviderBase.Import(Byte[] input)
       at PolicyEditor.ViewModels.ParticularSectionViewModel.get_ParDocument()
       at PolicyEditor.Views.ParticularsSection.Loadparticular(String param)}

    can you please advice me what i have to do.??


  2. Iva Toteva
    Iva Toteva avatar
    1319 posts

    Posted 08 Nov 2010 Link to this post

    Hi jaimin,

    In fact your implementation of the Import function works fine. It is the Export function that does not work correctly and the documents that it produces are not valid DOCX files. Thus the exception you get on Import.
    The straight-forward way to accomplish what you want is:

    Particular.wp_perticulars = provider.Export(value);
    Another option would be:
    provider.Export(value, output);
    Particular.wp_perticulars = output.ToArray();

    The GetBuffer() method, however, returns an array which can have allocated unused bytes as described in this MSDN article and it will no longer represent a valid DOCX document.

    Try this out and let us know if it helped.

    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top