web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Forums / Finance forum / How to convert stored ...
Finance forum

How to convert stored file to bit array in d365

(0) ShareShare
ReportReport
Posted on by

Hi

I am storing a file in a table. Field is container type, i want a class to convert this file and return the bit array.

With the below code it is throwing this error , pls suggest a fix  to this problem.

class fileInteractionBitarrayTest
{        
    /// <summary>
    /// Runs the class with the specified arguments.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {    
        TestXMLTable ltest;
        container fc;
        select Filecontainer from ltest;
        fc = ltest.Filecontainer;
        fileInteractionBitarrayTest myfileinttest = new fileInteractionBitarrayTest();
        str s =  myfileinttest.convertContainerToBytes(fc).ToString();
        info (s);
           
    }

    public  System.Byte[] convertContainerToBytes(container _container)
    {
        System.IO.MemoryStream  memStream;
        System.Byte[]           bytes;
        Binary                 assemblyBinary;

        if (_container)
        {
            try
            {
                assemblyBinary = Binary::constructFromContainer(_container);
                memStream = assemblyBinary.getMemoryStream();
                bytes = memStream.ToArray();
                memStream.Close();
            }
            catch (Exception::CLRError)
            {
                SRSProxy::handleClrException(Exception::Error);
            }

            return bytes;
        }

        return null;
    }

}

Microsoft.Dynamics.Ax.Xpp.InfoException: Error executing code: Binary (object), method constructFromContainer called with invalid parameters.
 at Microsoft.Dynamics.Ax.MSIL.Interop.throwException(Int32 ExceptionValue, interpret* ip)
 at Microsoft.Dynamics.Ax.MSIL.cqlClassIL.callReturn(Int32 rc, interpret* ip, Byte handleRef)
 at Microsoft.Dynamics.Ax.MSIL.cqlClassIL.CallStatic(String className, String methodName, Object[] parameters, Type[] types, Object[] varargs, Type[] varargsTypes)
 at Microsoft.Dynamics.Ax.Xpp.XppObjectBase.StaticCall(String className, String methodName, Object[] parameters, Type[] types, Object[] varargs)
 at Microsoft.Dynamics.Ax.Xpp.Binary.constructFromContainer(Object[] data)
 at Dynamics.AX.Application.fileInteractionBitarrayTest.`convertContainerToBytes(Object[] _container) in xppSource://Source/TestFileUpload\AxClass_fileInteractionBitarrayTest.xpp:line 29
 at Dynamics.AX.Application.fileInteractionBitarrayTest.fileInteractionBitarrayTestCoCHelper.`convertContainerToBytes(fileInteractionBitarrayTest instance, Object[] arg0)
 at Dynamics.AX.Application.fileInteractionBitarrayTest.convertContainerToBytes(Object[] _container)
 at Dynamics.AX.Application.fileInteractionBitarrayTest.`main(Args _args) in xppSource://Source/TestFileUpload\AxClass_fileInteractionBitarrayTest.xpp:line 14
 at Dynamics.AX.Application.fileInteractionBitarrayTest.fileInteractionBitarrayTestCoCHelper.`main(fileInteractionBitarrayTest instance, Args arg0)
 at Dynamics.AX.Application.fileInteractionBitarrayTest.main(Args _args)
 at fileInteractionBitarrayTest::Main(Object[] , Boolean& )
 at Microsoft.Dynamics.Ax.Xpp.ReflectionCallHelper.MakeStaticCall(Type type, String MethodName, Object[] parameters)
 at Dynamics.AX.Application.SysClassRunnerHelper.`runMainMethod(SysClassRunnerState state) in xppSource://Source/ApplicationFoundation\AxClass_SysClassRunnerHelper.xpp:line 22
 at Dynamics.AX.Application.SysClassRunnerHelper.SysClassRunnerHelperCoCHelper.`runMainMethod(SysClassRunnerHelper instance, SysClassRunnerState arg0)
 at Dynamics.AX.Application.SysClassRunnerHelper.runMainMethod(SysClassRunnerState state)
 at Dynamics.AX.Application.Forms.SysClassRunner.FormControl.timerControl.elapsed() in xppSource://Source/ApplicationFoundation\AxForm_SysClassRunner.xpp:line 52
Error executing code: Binary (object), method constructFromContainer called with invalid parameters.



  • Mea_ Profile Picture
    60,286 on at

    Code looks of for me, generally you get this error if container has invalid content, so check it.

  • Mav Profile Picture
    on at

    It’s a png file , how does it matter .

    The purpose of that container field is to store any file format.

  • Mea_ Profile Picture
    60,286 on at

    Yes but you cannot pass any container to Binary::constructFromContainer method, it should be in specific format.

  • Mav Profile Picture
    on at

    You mean container has to be blobtype , please be informed that earlier I have tried with blob type container and got the same error hence I switched to normal container , but still getting Same error.

  • Mav Profile Picture
    on at

    Code with blob data type and its subsequent error.  

    Apparently the same error as when using standard container type.

    class fileInteractionBitarrayTest
    {        
        /// <summary>
        /// Runs the class with the specified arguments.
        /// </summary>
        /// <param name = "_args">The specified arguments.</param>
        public static void main(Args _args)
        {    
            TestXMLTable ltest;
            Blobdata fc;
            select Filecontainer from ltest;
            fc = ltest.Filecontainer;
            fileInteractionBitarrayTest myfileinttest = new fileInteractionBitarrayTest();
            str s =  myfileinttest.convertContainerToBytes(fc).ToString();
            info (s);
               
        }
    
        public  System.Byte[] convertContainerToBytes(container _container)
        {
            System.IO.MemoryStream  memStream;
            System.Byte[]           bytes;
            Binary                 assemblyBinary;
    
            if (_container)
            {
                try
                {
                    assemblyBinary = Binary::constructFromContainer(_container);
                    memStream = assemblyBinary.getMemoryStream();
                    bytes = memStream.ToArray();
                    memStream.Close();
                }
                catch (Exception::CLRError)
                {
                    SRSProxy::handleClrException(Exception::Error);
                }
    
                return bytes;
            }
    
            return null;
        }
    
    }

    icrosoft.Dynamics.Ax.Xpp.InfoException: Error executing code: Binary (object), method constructFromContainer called with invalid parameters.
     at Microsoft.Dynamics.Ax.MSIL.Interop.throwException(Int32 ExceptionValue, interpret* ip)
     at Microsoft.Dynamics.Ax.MSIL.cqlClassIL.callReturn(Int32 rc, interpret* ip, Byte handleRef)
     at Microsoft.Dynamics.Ax.MSIL.cqlClassIL.CallStatic(String className, String methodName, Object[] parameters, Type[] types, Object[] varargs, Type[] varargsTypes)
     at Microsoft.Dynamics.Ax.Xpp.XppObjectBase.StaticCall(String className, String methodName, Object[] parameters, Type[] types, Object[] varargs)
     at Microsoft.Dynamics.Ax.Xpp.Binary.constructFromContainer(Object[] data)
     at Dynamics.AX.Application.fileInteractionBitarrayTest.`convertContainerToBytes(Object[] _container) in xppSource://Source/TestFileUpload\AxClass_fileInteractionBitarrayTest.xpp:line 29
     at Dynamics.AX.Application.fileInteractionBitarrayTest.fileInteractionBitarrayTestCoCHelper.`convertContainerToBytes(fileInteractionBitarrayTest instance, Object[] arg0)
     at Dynamics.AX.Application.fileInteractionBitarrayTest.convertContainerToBytes(Object[] _container)
     at Dynamics.AX.Application.fileInteractionBitarrayTest.`main(Args _args) in xppSource://Source/TestFileUpload\AxClass_fileInteractionBitarrayTest.xpp:line 14
     at Dynamics.AX.Application.fileInteractionBitarrayTest.fileInteractionBitarrayTestCoCHelper.`main(fileInteractionBitarrayTest instance, Args arg0)
     at Dynamics.AX.Application.fileInteractionBitarrayTest.main(Args _args)
     at fileInteractionBitarrayTest::Main(Object[] , Boolean& )
     at Microsoft.Dynamics.Ax.Xpp.ReflectionCallHelper.MakeStaticCall(Type type, String MethodName, Object[] parameters)
     at Dynamics.AX.Application.SysClassRunnerHelper.`runMainMethod(SysClassRunnerState state) in xppSource://Source/ApplicationFoundation\AxClass_SysClassRunnerHelper.xpp:line 22
     at Dynamics.AX.Application.SysClassRunnerHelper.SysClassRunnerHelperCoCHelper.`runMainMethod(SysClassRunnerHelper instance, SysClassRunnerState arg0)
     at Dynamics.AX.Application.SysClassRunnerHelper.runMainMethod(SysClassRunnerState state)
     at Dynamics.AX.Application.Forms.SysClassRunner.FormControl.timerControl.elapsed() in xppSource://Source/ApplicationFoundation\AxForm_SysClassRunner.xpp:line 52
    Error executing code: Binary (object), method constructFromContainer called with invalid parameters.


  • Mav Profile Picture
    on at

    Anyone any thoughts ?

  • Mav Profile Picture
    on at

    Anyone any thoughts . Hopefully it is possible to convert file to a bit array in d365?

  • Martin Dráb Profile Picture
    240,877 Most Valuable Professional on at

    Your method doesn't give me any exception when used with EcoResProductImage.ThumbnailSize:

    public static void main(Args _args)
    {
        EcoResProductImage productImage;
        container fc;
        select firstonly ThumbnailSize from productImage;
        fc = productImage.ThumbnailSize;
        fileInteractionBitarrayTest myfileinttest = new fileInteractionBitarrayTest();
        str s =  myfileinttest.convertContainerToBytes(fc).ToString();
        info (s);           
    }

    Therefore the question of what data you have in TestXMLTable.Filecontainer still seems to be very relevant. How do you put values there?

    And by the way, it's a byte array, not a bit array.

  • Mav Profile Picture
    on at

    Thanks for responding.

    I have a png file in TestXMLTable.Filecontainer

    I use the file upload control 's OnUploadCompleted() event to trigger below code which put values in the TestXMLTable.Filecontainer

    public void OnUploadCompleted()
            {
                TestXMLTable ltesttable;
    
                FileUploadTemporaryStorageResult    result = this.getFileUploadResult() as FileUploadTemporaryStorageResult;
                  
                super();
    
                if(result && result.getUploadStatus())
                {
                    fileurl = result.getDownloadUrl();             
                    info (fileurl);
                    using(System.IO.MemoryStream stream = result.openResult() as System.IO.MemoryStream)                
                    using(System.IO.StreamReader  sreader = new System.IO.StreamReader(stream, System.Text.Encoding::UTF8, true))
                    {
                             
    
                        str s;
                        Blobdata myFile;          
                        s = sreader.ReadToEnd();
                        info (s);
                        myFile = str2con(s);
                        
                        if(myFile)
                        {
                            ttsbegin;
                            //testXml.clear();
                            ltesttable.uploaddate = DateTimeUtil::utcNow();
                            ltesttable.Filecontainer = myFile;
                            ltesttable.FileURL = fileurl;
                            ltesttable.insert();
                            ttscommit;
                        }
    
                       
    
                    }               


  • Verified answer
    Mea_ Profile Picture
    60,286 on at

    As I said before, it's probably caused by the way you save data, instead of converting it to string and then string to container, you can get container from memory stream, like

    binaryData = Binary::constructFromMemoryStream(mstream);

    if (binaryData)

    {

       con = binaryData.getContainer();

    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Finance

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans