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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Preview issue of PDF attachment created from base64string

(0) ShareShare
ReportReport
Posted on by 35

Dear community

I am facing issues with preview of pdf documents created per code from base64string 

 2020_2D00_10_2D00_27-14_5F00_20_5F00_21_2D00_C_5F005F00_Tasks_5F00_Screenshots_5F00_2020_2D00_10_2D00_27-12_5F00_38_5F00_21_2D00_Posteingang-_2D00_-pavel.stikhin_4000_senergy.ch-.png

Nevertheless, this document can be opened and looks good so far.

The mentioned issue comes in my tests, when I

1) take an existing pdf (manually created DocuRef on an TrvExpenses entry) 

2) encode it to base64string

3) decode and create a new DocuRef from this base64string by using DocumentManagement::attachFile()

Here is the FO code

pastedimage1603807102690v1.png

where docuRef is an existing DocuRef record from point 1.

When I don't use DocumentManagement::attachFile(), but create a file directly:

pastedimage1603807236850v2.png

and make an upload of the file manually (on the same TrvExpenses entry), the preview is looking properly:

pastedimage1603807833109v4.png 

I have checked the links:

https://dynamicsuser.net/ax/f/developers/96818/attach-files-programmatically-using-x

https://community.dynamics.com/365/financeandoperations/f/dynamics-365-for-finance-and-operations-forum/386758/converting-pdf-file-into-base64string-in-d365-finance-and-operations

Could you please help / advise me?

Thank's in advance!

Pavel

I have the same question (0)
  • Verified answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    Did I understand correctly, that your problem is not related to base64 at all? But rather it's related to using DocumentManagement::attachFile?

    My first suggestion is to compare the two DocuRef records (one created by your code, one by attaching the file via UI) and trying to spot differences. Most likely there's some issue in the parameters that you are passing to DocumentManagement::attachFile method.

  • Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    Your screenshot doesn't show values of your variables, therefore we can't review if you're using correct values.

    The first one I would check is _fileContentType.

  • pavel.stikhin Profile Picture
    35 on at

    Hi Nikolaos

    Yes, the problem is related to using DocumentManagement::attachFile(). Another (similar) attempt with the same wrong preview behaviour was done with the code (using docuActionArchive.attachFile()) - below.

    I will check the differences between the two records.

    axDocuRef = AxDocuRef::construct();

               axDocuRef.parmRefTableId(docuRef.RefTableId);

               axDocuRef.parmRefRecId(docuRef.RefRecId);

               axDocuRef.parmRefCompanyId(docuRef.ActualCompanyId);

               axDocuRef.parmTypeId(DocuType.TypeID);

               axDocuRef.parmName(docExchTmp.FileName);

               axDocuRef.save();

               docuRefNew = axDocuRef.docuRef();

               docuActionArchive = DocuAction::newDocuRef(docuRefNew);

               docuActionArchive.attachFile(docuRefNew, docExchTmp.FileName + "." + docExchTmp.FileType, docExchTmp.FileType, stream);

  • pavel.stikhin Profile Picture
    35 on at

    Yes, I didn't given all the technical (auxiliary) details there. But generally speaking, can you reproduce correct behaviour of a pdf preview by using DocumentManagement::attachFile() ?

  • nmaenpaa Profile Picture
    101,160 Moderator on at

    Could you please investigate it on your side, since you reported the issue? Did you already compare the two DocuRef records as suggested?

  • Verified answer
    Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    Could you please tell us what MIME type are you passing to _fileContentType parameter? As I said, that's the first thing I would check.

  • pavel.stikhin Profile Picture
    35 on at

    I am observing only one relelated difference in  the filed REFCOMPANYID. Updating it by direct sql update doesn't change anything.

  • pavel.stikhin Profile Picture
    35 on at

    You are right Martin! The origin of the issue was the value 'pdf' instead of 'application/pdf'. After I have changed the code (below), the desired preview started working properly. I thank you very much for your help and constructive tips!

    FreeText    fn = docExchTmp.FileName + "." + docExchTmp.FileType;

               var ms = System.Web.MimeMapping::GetMimeMapping(fn);

               DocumentManagement::attachFile(docuRef.RefTableId

                                            , docuRef.RefRecId

                                            , docuRef.ActualCompanyId

                                            , DocuType.TypeID

                                            , stream

                                            , System.IO.Path::GetFileName(fn)

                                            , ms

                                            , System.IO.Path::GetFileNameWithoutExtension(fn));

  • pavel.stikhin Profile Picture
    35 on at

    Nikolaos, I would like to thank you very much for your help and constructive questions  /  ideas!

  • Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    Great!

    By the way, please use Insert > Insert Code (in the rich-formatting view) to paste source code. The result will be easier to read and copy, and we can easily refer to line numbers. For example:

    FreeText fn = docExchTmp.FileName   "."   docExchTmp.FileType;
    var ms = System.Web.MimeMapping::GetMimeMapping(fn);
    DocumentManagement::attachFile(docuRef.RefTableId
    							, docuRef.RefRecId
    							, docuRef.ActualCompanyId
    							, DocuType.TypeID
    							, stream
    							, System.IO.Path::GetFileName(fn)
    							, ms
    							, System.IO.Path::GetFileNameWithoutExtension(fn));

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 592 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 478 Super User 2025 Season 2

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 305 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans