Upload Multiple Files from RTC

b_singhb_singh Posts: 1Member
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;
    VAR
      [email protected] : TEMPORARY Record 99008535;
      [email protected] : Codeunit 50020;
      [email protected] : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Collections.IEnumerator" RUNONCLIENT;
      [email protected] : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Collections.ArrayList" RUNONCLIENT;
      [email protected] : DotNet "'System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Windows.Forms.OpenFileDialog" RUNONCLIENT;
      [email protected] : DotNet "'System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Windows.Forms.DialogResult" RUNONCLIENT;
      [email protected] : DotNet "'mscorlib, Version=2.0.0.0, 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;
    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.