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 UploadMultipleFiles@1000000019(UploadToPath@1000000001 : Text[250];FileFilter@1000000005 : Text[30]) : Boolean;
    VAR
      BLOBRef@1000000002 : TEMPORARY Record 99008535;
      Strings@1000000004 : Codeunit 50020;
      Enumerator@1000000010 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Collections.IEnumerator" RUNONCLIENT;
      ArrayList@1000000011 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Collections.ArrayList" RUNONCLIENT;
      FileDialog@1000000007 : DotNet "'System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Windows.Forms.OpenFileDialog" RUNONCLIENT;
      DialogResult@1000000006 : DotNet "'System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Windows.Forms.DialogResult" RUNONCLIENT;
      FileInfo@1000000016 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.FileInfo" RUNONCLIENT;
      SelectedFile@1000000012 : Text[1024];
      ToFilePath@1000000000 : Text[1024];
      "3TierMgt"@1000000015 : Codeunit 419;
      ClientTempFileName@1000000017 : Text[1024];
      FilesUploaded@1000000009 : Integer;
    BEGIN
      // ************************************************  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();
      ArrayList.AddRange(FileDialog.FileNames());
      Enumerator := ArrayList.GetEnumerator();

      WHILE Enumerator.MoveNext() DO BEGIN

        CLEAR(BLOBRef.Blob);
        CLEAR(FileInStream);
        CLEAR(FileOutStream);
        CLEAR(FileInfo);

        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,'.'));
        FileInfo.CopyTo(ClientTempFileName);

        // 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.CREATEOUTSTREAM(FileOutStream);
          COPYSTREAM(FileOutStream,FileInStream);
          BLOBRef.Blob.EXPORT(ToFilePath);  // Not wrapping in IF condition because I want it to throw descriptive error. e.g permissions
        END;
        FilesUploaded += 1;
        // Removing the temp file
        FileInfo := FileInfo.FileInfo(ClientTempFileName);
        FileInfo.Delete();
      END;
      IF (FilesUploaded>0) AND GUIALLOWED THEN BEGIN
        MESSAGE('%1 files uploaded out of %2',FilesUploaded,ArrayList.Count);
        EXIT(TRUE);
      END;
    END;



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.