Options

Downloading a file from an URL in NAV 2009 R2

navision2009R2navision2009R2 Member Posts: 8
I need to download a file, from an URL, in NAV 2009 R2.

I'm trying to do the following:
- request a list of files from a server using a XMLRequest => DONE
- list the files on a form so that users can select the desired one => DONE
- download the file from the same server into the users' TEMPORARYFOLDER => TODO.

How can I proceed ?

PS:
- I tried using a DotNet variable, but itt seems like on cannot use System.Net.HttpWebResponse in NAV 2009 R2
- I succed in opennig the file in iexplore using HYPERLINK(myUrl) so netwok config is OK
- Most of the time, files will be PDF ones, but it could also be so other file formats. You can assume I know the file format of the one need to download.
- The files are on an on-premise server, so you can assume downloading thoses files are safe.

Best Answer

  • Options
    ftorneroftornero Member Posts: 522
    Answer ✓
    Hello @navision2009R2 ,

    You can use something like this:
    OBJECT Codeunit 50001 Download File from URL
    {
      OBJECT-PROPERTIES
      {
        Date=28/11/20;
        Time=23:02:06;
        Modified=Yes;
        Version List=DL FILE;
      }
      PROPERTIES
      {
        OnRun=BEGIN
              END;
    
      }
      CODE
      {
        VAR
          Client@1000000000 : Boolean;
    
        PROCEDURE DownLoadFile@1000000012(URL@1000000000 : Text[1024];FileName@1000000008 : Text[1024]);
        VAR
          XmlHttp@1000000003 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 6.0:{88D96A0B-F192-11D4-A65F-0040963251E5}:'Microsoft XML, v6.0'.ServerXMLHTTP60";
          InStr@1000000001 : InStream;
          OutStr@1000000007 : OutStream;
          CompInf@1000000002 : TEMPORARY Record 79;
        BEGIN
          IF ISCLEAR(XmlHttp) THEN
            CREATE(XmlHttp, FALSE, NOT ISSERVICETIER);
    
          XmlHttp.open('GET', URL,0);
          XmlHttp.setTimeouts(10000,10000,10000,100000);
          XmlHttp.send('');
    
          IF XmlHttp.status = 200 THEN BEGIN
            InStr := XmlHttp.responseStream;
            CompInf.Picture.CREATEOUTSTREAM(OutStr);
            IF COPYSTREAM(OutStr, InStr) THEN
              CompInf.Picture.EXPORT(FileName);
          END;
    
          CLEAR(XmlHttp);
        END;
    
        BEGIN
        END.
      }
    }
    
    

    Regards

Answers

  • Options
    Etimos90Etimos90 Member Posts: 2
    hi ,
    Can you use automation variable with create(automation,true,true.


    Hope this can help you
  • Options
    ftorneroftornero Member Posts: 522
    Answer ✓
    Hello @navision2009R2 ,

    You can use something like this:
    OBJECT Codeunit 50001 Download File from URL
    {
      OBJECT-PROPERTIES
      {
        Date=28/11/20;
        Time=23:02:06;
        Modified=Yes;
        Version List=DL FILE;
      }
      PROPERTIES
      {
        OnRun=BEGIN
              END;
    
      }
      CODE
      {
        VAR
          Client@1000000000 : Boolean;
    
        PROCEDURE DownLoadFile@1000000012(URL@1000000000 : Text[1024];FileName@1000000008 : Text[1024]);
        VAR
          XmlHttp@1000000003 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 6.0:{88D96A0B-F192-11D4-A65F-0040963251E5}:'Microsoft XML, v6.0'.ServerXMLHTTP60";
          InStr@1000000001 : InStream;
          OutStr@1000000007 : OutStream;
          CompInf@1000000002 : TEMPORARY Record 79;
        BEGIN
          IF ISCLEAR(XmlHttp) THEN
            CREATE(XmlHttp, FALSE, NOT ISSERVICETIER);
    
          XmlHttp.open('GET', URL,0);
          XmlHttp.setTimeouts(10000,10000,10000,100000);
          XmlHttp.send('');
    
          IF XmlHttp.status = 200 THEN BEGIN
            InStr := XmlHttp.responseStream;
            CompInf.Picture.CREATEOUTSTREAM(OutStr);
            IF COPYSTREAM(OutStr, InStr) THEN
              CompInf.Picture.EXPORT(FileName);
          END;
    
          CLEAR(XmlHttp);
        END;
    
        BEGIN
        END.
      }
    }
    
    

    Regards
Sign In or Register to comment.