Pages

Friday, January 20, 2017

Import a File and Export it as a ZIP file in NAV 2017

Hi guys,

I did some R&D on the File Management codeunit (419) which is available in NAV and found out that it has pretty much all the functions related to files available in it.

For example: For zipping a file, we no longer need third party software to do the job for us in NAV.

Like this, there are many more functions available in this codeunit which we can use to our advantage and play with 'em. I have used some of the functions in an example which I will demonstrate below,

Before starting, I want you to know that this post is not limited to just importing and zipping a file. I want to show you the ability of codeunit 419 and the functions available in it. I have used a couple of them in my code.


I have developed a codeunit. When I run this codeunit object, a dialog opens up to select a file and then we may select any file of any type and NAV will import it and export it in the same folder as a ZIP file. 

STEP 1: Run the codeunit which opens up a dialog window as shown below,



STEP 2: Check the folder from which I selected the file. I see a ZIP file as shown below,



How did I accomplish this?? Create a new codeunit and declare the following Variables,

Variables
Name                           DataType    Subtype                  Length
FileMgt                      Codeunit     File Management   
ServerFileName        Text       
ClientFileName         Text       
ZipFileName             Text       

Code >>>

CLEAR(FileMgt);

//Get fob file on client machine

ClientFileName := FileMgt.OpenFileDialog('Import a Word file', '', 'Word Files (*.doc)|*.doc|All Files (*.*)|*.*');

//Upload fob file to server and get the full path of the file
ServerFileName := FileMgt.UploadFileSilent(ClientFileName);

//Create a new zip file

ZipFileName := FileMgt.CreateZipArchiveObject();

//Add the fob file to the zip file
FileMgt.AddFileToZipArchive(ServerFileName,FileMgt.GetFileName(ClientFileName));

//Close the zip file
FileMgt.CloseZipArchive;

//Download the zip file onto the client machine, saving it to the same directory where the
//fob file is located, and giving it the same name but with a .zip extension

FileMgt.DownloadToFile(ZipFileName,
FileMgt.CombinePath(FileMgt.GetDirectoryName(ClientFileName),
FileMgt.GetFileNameWithoutExtension(ClientFileName)) + '.zip');

Code <<<

NOTE: For the demo above, I used word file type to import. You may use any other files types like .fob (NAV Object Files) , .txt (Text), .xlsx (Excel Files) etc. You just need to replace the parameters in the OpenFileDialog function in the code above.

If you want to work with other file types replace the line written below in the code above,

//For FOB  Files
_clientFileName := _fileMgt.OpenFileDialog('Import a FOB file', '', 'FOB Files (*.fob)|*.fob|All Files (*.*)|*.*');


//For Excel Files
_clientFileName := _fileMgt.OpenFileDialog('Import a Excel file', '', 'Excel Files (*.xlsx)|*.xlsx|All Files (*.*)|*.*');


That's all for this post!
Hope you find this post useful.😊 Do comment your thoughts and doubts in the comments section below.
Keep learning!

1 comment: