I am trying to convert PDF attachment of Sales order invoice into Base64 string.
Below is my code snippet and I referred below blog for same,
https://www.linkedin.com/pulse/get-document-management-attached-file-api-integration-khan/
I converted same file to Base64 using below 2 ways
However, I am getting some different value in fileBase64Str variable.
let me know how it can be fixed. if any other alternative way I can opt for?
internal final class ITSGetAttachedFileRunnabl{ /// <summary> /// Class entry point. The system will call this method when a designated menu /// is selected or when execution starts and this class is set as the startup class. /// </summary> /// <param name = /_args/>The specified arguments.</param> public static void main(Args _args) { DocuRef docuref; ITSGetFileFromDocMgmtInVariousFormats runnable = ITSGetFileFromDocMgmtInVariousFormats::construct(); select docuref where docuref.RecId == 68719599267; runnable.readfromDocuRefAttachments(docuref); }}
using Microsoft.Dynamics.ApplicationPlatform.Services.Instrumentation;using Microsoft.DynamicsOnline.Infrastructure.Components.SharedServiceUnitStorage;using Microsoft.Dynamics.AX.Framework.FileManagement;using Microsoft.WindowsAzure.Storage;Using Microsoft.WindowsAzure.Storage.Blob;internal final class ITSGetFileFromDocMgmtInVariousFormats{ str docfiletype; Microsoft.Dynamics.AX.Framework.FileManagement.IDocumentStorageProvider storageProvider; Public void readfromDocuRefAttachments(DocuRef _docuRef) { Blobdata blobContainer; AsciiStreamIo file; container record; str downloadUrl; // Grant clrinterop permission. new InteropPermission(InteropKind::ClrInterop).assert(); if (_docuRef.isValueAttached()) { var docuValueloc = _docuRef.docuValue(); downloadUrl = docuValueloc.Path; if (!downloadUrl || docuValueloc.Type == DocuValueType::Others) { str accessToken = DocumentManagement::createAccessToken(_docuRef); downloadUrl = Microsoft.Dynamics.AX.Framework.FileManagement.URLBuilderUtilities::GetDownloadUrl(docuValueloc.FileId, accessToken); } storageProvider = Docu::GetStorageProvider(_docuRef.docuType(), false); var docContents = storageProvider.GetFile(docuValueloc.createLocation()); file = AsciiStreamIo::constructForRead(docContents.Content); // download file on browser file.read(); str displayUrl = DocumentManagement::getAttachmentPublicUrl(_docuref); Browser br = new Browser(); br.navigate(displayUrl); // To byte - working System.IO.StreamReader streamReader = new System.IO.StreamReader(docContents.Content); System.Byte[] bytes; System.Text.Encoding getUTF8 = System.Text.Encoding::get_UTF8(); //bytes = getUTF8.GetBytes(streamReader.ReadToEnd()); bytes = getUTF8.GetBytes(streamReader.ReadToEnd()); //info ('Byte ready'); // To memorystream System.IO.MemoryStream stream = new System.IO.MemoryStream(bytes); //info ('stream ready'); // to binary and blob container Binary binaryData = Binary::constructFromMemoryStream(stream); //info ('binary ready'); if (binaryData) { blobContainer = binaryData.getContainer(); //info ('Container ready'); } // to base 64 str fileBase64Str = con2base64str(blobContainer); info (strFmt('base: %1', fileBase64Str)); //info (/Base64 ready/); DocuValue docuValue; select firstonly docuValue where docuValue.RecId == _docuRef.ValueRecId; info (strFmt('Original filename is %1', docuValue.originalFileName)); } } public static ITSGetFileFromDocMgmtInVariousFormats construct() { return new ITSGetFileFromDocMgmtInVariousFormats(); }}