Upload Multiple Files from RTC

b_singhb_singh Member Posts: 1
edited 2019-06-24 in NAV Three Tier
Recently, I was given task to be able to upload multiple files onto Navision server instance from RTC(2009 R2). I found it to be very interesting because Navision(2009 R2) doesn't provide inbuilt functionality. Had some problems because of 3tier atchitecture. Though I'd share with others in case anyone needed to do the same. Off-course you can improve this with your own ideas. Here is how I did this:

FYI.. I used DotNet classes.
    PROCEDURE [email protected]([email protected] : Text[250];[email protected] : Text[30]) : Boolean;
      [email protected] : TEMPORARY Record 99008535;
      [email protected] : Codeunit 50020;
      [email protected] : DotNet "'mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Collections.IEnumerator" RUNONCLIENT;
      [email protected] : DotNet "'mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Collections.ArrayList" RUNONCLIENT;
      [email protected] : DotNet "'System.Windows.Forms, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Windows.Forms.OpenFileDialog" RUNONCLIENT;
      [email protected] : DotNet "'System.Windows.Forms, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Windows.Forms.DialogResult" RUNONCLIENT;
      [email protected] : DotNet "'mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.FileInfo" RUNONCLIENT;
      [email protected] : Text[1024];
      [email protected] : Text[1024];
      "3TierMgt"@1000000015 : Codeunit 419;
      [email protected] : Text[1024];
      [email protected] : Integer;
      // ************************************************  Parameter definations  ********************************************************
      // UploadToPath -> Path of the folder in which user want to upload the file
      // FileFilter   -> Examples All Files (*.*)|*.*, Text Files (*.txt)|*.txt, Excel (*.xl*)|*.xl* etc
      // ************************************************      Description        ********************************************************
      // This function open the file selector and user select the file that get uploaded into temporay BLOB field
      // blob then gets exported to UploadToPath with same file name as selected
      // *********************************************************************************************************************************
      FileDialog := FileDialog.OpenFileDialog();
      FileDialog.Multiselect := TRUE;
      FileDialog.Title := 'Select multiple files';
      FileDialog.Filter := FileFilter;

      // Show multiple file selector
      DialogResult := FileDialog.ShowDialog();
      ArrayList := ArrayList.ArrayList();
      Enumerator := ArrayList.GetEnumerator();

      WHILE Enumerator.MoveNext() DO BEGIN


        SelectedFile := Enumerator.Current;   // Select Current File
        FileInfo := FileInfo.FileInfo(SelectedFile);    //Fileinfo is DotNet object
        // When doing silent upload(means no navision file selector), then file has to be in RTC's temp folder
        ClientTempFileName := "3TierMgt".ClientTempFileName('',Strings.TokenFromEnd(SelectedFile,'.'));

        // MagicPath allow to hide selector dialog forUPLOADINTOSTREAM
        IF UPLOADINTOSTREAM('', "3TierMgt".Magicpath(), '', ClientTempFileName, FileInStream) THEN BEGIN
          // Create the drop file path
          ToFilePath := STRSUBSTNO('%1\%2',UploadToPath,FileInfo.Name);

          BLOBRef.Blob.EXPORT(ToFilePath);  // Not wrapping in IF condition because I want it to throw descriptive error. e.g permissions
        FilesUploaded += 1;
        // Removing the temp file
        FileInfo := FileInfo.FileInfo(ClientTempFileName);
      IF (FilesUploaded>0) AND GUIALLOWED THEN BEGIN
        MESSAGE('%1 files uploaded out of %2',FilesUploaded,ArrayList.Count);

Variable UploadToPath contains path to shared folder on server in my case. Found it to be easiest. In case you need to drop the file on filesystem on server then I think you will have to receive the file into temp folder on server also and then move the file into desired folder. Didn't try this bit myself.
Sign In or Register to comment.