Getting a Base64 codified PDF from WebService

Hi everyone,

I’m doing a developmnet that needs to download a base64 PDF codified from a webService. I call correctly to the WS, and its answer is this one. As you can see. in the node “v1:strEtiquetas” I get the label codified in Base64:

My idea is to save that value in a blob field (sticker) of one of my tables:

save.png

For testing that I’m getting all the data correctly, my nex step is to save the PDF in my computer:

exporta.png

But the generated PDF is in blanck, 0kb, empty…

Does anyone see where can I be making any fail?

THnak you very much

Hi aitorNAV,

Try to use TempBlob Record and function FromBase64String to get the file.

Thnaks for your answer,

As you can see in the 2nd image if uploaded, I’m already using the “FromBase64String” function. The “sticker” field from shipment is a blob type field.

What is the difference that are you talking about?

Thank you again.

Hi aitorNAV

Sorry I think you’re using AL, and that’s the way to convert base 64 from files received.

I made a extensión a couple of months ago, and I decode a base 64 file from web Service without problems.

Can you show us the variable definitions?

Yes, I’m using AL.

Which variable definitions are you asking for?


Type	DotNet	System.Array.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
lEtiqueta	BigText		
MemoryStream	DotNet	System.IO.MemoryStream.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
OStream	OutStream		
IStream	InStream		

ASk for anything you need

Thank you

OK.

So I think you’re still coding in development enviroment (C/AL), cause in extension development DotNet isn’t allow (only for on-premises extensión)

Add the following Italic line to your code and try again:

ShipHeader.Sticker.CREATEOUTSTREAM(OStream);
MemoryStream.WriteTo(OStream);
ShipHeader.Registered := TRUE;

Thank you very much, it worked! Really appreciated!

Another question with this issue. My intention is to print the received PDF in a printer. Now, what I’m dping is to download it with this code:

lShipment.Sticker.CREATEINSTREAM(IStream);
   ToFile :='Sticker.pdf';
   DOWNLOADFROMSTREAM(IStream,'','<TEMP>','',ToFile);

But as you will know, this is saveing the PDF into a temp folder, that dissapears when the NAV session is closed. Does exist any way to download it to a folder indicated by me? Or may be, without saving the PDF phisically, to print it directly using the saved blob?

Thank you very much

Hi aitorNAV,

The DOWNLOADFROMSTREAM function allow you to save the file in a folder in the client side, so you can modify the ‘’ directory for the path you want to save the file.

I think it’s better to save the file to a network location for share the file with other users.

Hope this help

Thanks Psice,

What I’ve tried is to use

   lShipment.Sticker.CREATEINSTREAM(IStream);
   ToFile :='Sticker.pdf';
   //DOWNLOADFROMSTREAM(IStream,'','<TEMP>','',ToFile); //descarga silenciosa
   DOWNLOADFROMSTREAM(IStream,'','C:\XML','',ToFile); //descarga silenciosa

But instead os saving in the indicated path, it opens the dialog window for saving or opening the PDF file

Hi aitorNAV,

The only way to not get de dialog it’s to use the Magic Path (), If you want to save to a specific folder, dialog box always be prompted.

Thanks Psice,

The Magic path is usable out of the stream function, for example, to copy from there to a known path?

Or may be I’m think aboput using the FIle management Codunit. Do you thikn that will be possible with that?

Muchas gracias!

Hi aitorNAV,

You can try MoveAndRenameClientFile function from File Management codeunit after download the file to magic path.

Hi,

ANd how can I know the path value for the MoveAndRenameClientFile function?

lFileManagement.MoveAndRenameClientFile('<TEMP>','pegatina.pdf','C:\XML\');

Hi aitorNAV,

This is and old post, but it’s quite useful for your request

https://blogs.msdn.microsoft.com/nav/2013/08/09/nav-pattern-of-the-week-silent-file-upload-and-download/

Hello again,

I already try to use the code that found last week on that page, like this:


   lShipment.CALCFIELDS(Sticker);
   lTmpBLob.Blob := lShipment.Sticker;
   lTempServerFileName := lFileManagement.ServerTempFileName('.pdf');
   lFileManagement.DownloadToFile(lTempServerFileName,'c:\XML\pegatina.pdf');

But I find this error:

6355.error.png

I’m debuging the development, and the error appears in this pint of the 419 CodeUnit:

6355.debug.png

Hi AitorNAV,

Well, I think the best option is to use BLOBExport and then MoveAndRenameClientFile.

REally appreciated,

My issue with this is that, as I’ve said before, can’t use ‘<TEMP’> in the MoveAndRenameClientFile:

   lShipment.CALCFIELDS(Sticker);
   lTmpBLob.Blob := lShipment.Sticker;
   lTempServerFileName := lFileManagement.ServerTempFileName('.pdf');
   lFileManagement.BLOBExportToServerFile(lTmpBLob,lTempServerFileName);
  lFileManagement.MoveAndRenameClientFile('<TEMP>','pegatina.pdf','C:\XML\')

I get the error “’” file doesn’t exist…

Try going to a command line and type ECHO %TEMP% to see if you have a TEMP-variable.

But before you do anything With kind of temporary path do you need.

IF it is WINDOWS TEMP path then use

TempPathVar := ENVIRON(‘TEMP’);

Dynamics NAV temp path

TEMPORARYPATH

You could also use some code to get a temporarypath

Name DataType Subtype
environment DotNet System.Environment.‘mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’

MESSAGE(’%1’,environment.GetEnvironmentVariable(‘TEMP’));

Thanks [mention:74275055534c437ab13054c27a1ed5fb:e9ed411860ed4f2ba0265705b8793d05] and [mention:97f6ed63e0014905a055e14ec8b51571:e9ed411860ed4f2ba0265705b8793d05]. I’ve solved this issue with this code:

   lShipment.CALCFIELDS(Sticker);
   lTmpBLob.Blob := lShipment.Sticker;
   lTempServerFileName := lFileManagement.ServerTempFileName('.pdf');
   lFileManagement.BLOBExportToServerFile(lTmpBLob,lTempServerFileName);
   lFileManagement.DownloadToFile(lTempServerFileName,  'c:\XML\pegatina.pdf');

Thanks again for your tips, realy appreciated!