Download File From NAV Client, into SERVER.

BlackD1eceBlackD1ece Member Posts: 1
edited 2017-01-31 in NAV Three Tier
Hello m8ts, So this is my first post and I hope I'm not duplicating it. I've tried countless times to look internet for a solution (None was fully compatible whatsoever).

I have to create a tool wich first thing it needs to do is download a GZ file from an URL, but I'm not able to find the solution in any place.

I've found 2 so far:

1 - Using ADOStream:

URLINDEX := 'SOME URL';

IF(ISCLEAR(MXH)) THEN
CREATE(MXH,FALSE,TRUE);
MXH.open('GET',URLINDEX,FALSE);
MXH.send('');

IF MXH.status <> 200 THEN BEGIN
MESSAGE('ERROR');//TEST MESSAGE
CurrReport.BREAK;
END;

FileName := MXH.getResponseHeader('Content-Disposition');

IF STRPOS(FileName,'filename=') = 0 THEN
FileName :=''
ELSE BEGIN
FileName := COPYSTR(FileName,STRPOS(FileName,'filename=') + 10);

IF ISCLEAR(ADOStream) THEN
CREATE(ADOStream,FALSE,TRUE);

ADOStream.Type := 1;
ADOStream.Open;
ADOStream.Write(MXH.responseBody);

// ---> This line is commented from the online code found, because It wouldn't pass through here It would always close the stream. The url inputted is correct, and returns a download file if opened on a Browser.
//IF ADOStream.State = 1 THEN
//ADOStream.Close;

FileName := FICHEROS + FileName;
MESSAGE(FILENAME);
//CODE BREAKS HERE, IN SAVE FILE
ADOStream.SaveToFile(FileName,2);

//CURREPORT.BREAK;
ADOStream.Close;
END;

EDIT: Variables:
MXH Automation 'Microsoft XML, v6.0'.XMLHTTP60
ADOSTREAM Automation 'Microsoft ActiveX Data Objects 2.8 Library'.Stream
FILESYSTEM Automation 'Windows Script Host Object Model'.FileSystemObject
FILENAME Text
FILEPATH Text

This will only execute "Error when writing to the file.". And as told URL and Locations are fine.


2 - Using InStream / OutStream.

URLINDEX := 'some url';

IF(ISCLEAR(MXH)) THEN
CREATE(MXH,FALSE,TRUE);
MXH.open('GET',URLINDEX,FALSE);
MXH.send();

IF MXH.status <> 200 THEN
MESSAGE('ERROR URL'); //MESSAGE FOR TESTING

FICHERO.CREATE(FICHEROS+'index_0.gz');
FICHERO.CREATEOUTSTREAM(ONS);
INS := MXH.responseStream;
COPYSTREAM(ONS,INS);
FICHERO.CLOSE;

EDIT:Variables
FICHERO File
MXH Automation 'Microsoft XML, v6.0'.XMLHTTP60
INS InStream
ONS OutStream

This won't work, he's not able to transform Automation on InStream.

Those codes, are from the internet modified (not mine) to get to do what I need.

Working under NAV 2016. Also tried a CodeUnit from a WebPage wich is suposed to download files (http://www.dynamics.is/?attachment_id=584), also unusable for our systems.

This thing is driving me so crazy I might end with 4 JaggerMeister bottles under a bridge hallucinating with nav streams.

Thank you very much,

Answers

  • kaspermoerchkaspermoerch Member Posts: 43
    What version of NAV are you running on?
  • krikikriki Member, Moderator Posts: 9,112
    [Topic moved from 'NAV/Navision Classic Client' forum to 'NAV Three Tier' forum]

    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • RockWithNAVRockWithNAV Member Posts: 1,139
    Did you experience File Management Codeunit. I believe you need to go through once it will do your Job.
  • AitorEGAitorEG Member Posts: 342
    Hi!
    Did you get any solution? I'm finding the same situation, I need to download a PDF file from an URL. This URL is a field of the "Item" table. Did you get the way to download an external file?

    Thank you very much!
  • DuikmeesterDuikmeester Member Posts: 307
    Simplest method would be using WebClient.DownloadFile.
  • AitorEGAitorEG Member Posts: 342
    Simplest method would be using WebClient.DownloadFile.

    Thanks for your quick answer.
    As I've seen, you recommend me to use C# code. That wouldn't be any problem, but I don't know how to use it in my approach, may be I've not explain myself correctly. The thing is that, I need to download the PDFs from NAV2013. The idea is to download the PDFs to a temp folder, when I create a quote for one customer, and after, include them on an email, that is going to be sent to the customer.
    is this possible?

    Thank you!
  • vaprogvaprog Member Posts: 1,139
    Simplest method would be using WebClient.DownloadFile.
    AitorEG wrote: »
    As I've seen, you recommend me to use C# code.

    Duikmeester recomended you a .NET class. You can use in in C/AL by declaring a variable of type DotNet and subtype
    System.Net.WebClient.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    

    Thanks @Duikmeester for this info.
  • AitorEGAitorEG Member Posts: 342
    vaprog wrote: »
    Simplest method would be using WebClient.DownloadFile.
    AitorEG wrote: »
    As I've seen, you recommend me to use C# code.

    Duikmeester recomended you a .NET class. You can use in in C/AL by declaring a variable of type DotNet and subtype
    System.Net.WebClient.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    

    Thanks @Duikmeester for this info.

    Thanks both of you for the help. I'm finding some problems to apply what you have said.
    I have created a function in a CU we use for mending functions, like this:
    wmscqogj5023.png
    The local variables:

    WebClient DotNet System.Net.WebClient.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    URL Text 100

    The subtype of the DotNet is, as you have said,
    System.Net.WebClient.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    

    When I run the codeUnit, I see the next error:
    kv8k42c4bcgv.png
    The instance for the DotNet variable hasn't been created. It's trying to be called from System.Net.WebClient.DownloadFile in CodeUnit Mending Functions: DownloadPDFFronURL

    What I'm doing wrong?
  • vaprogvaprog Member Posts: 1,139
    Hi,
    Use the Constructor method to initialize the variable:
    WebClient := WebClient.WebClient;
    

    Some classes in .NET are static and can be used without construction, but most need to be initialized.
  • AitorEGAitorEG Member Posts: 342
    vaprog wrote: »
    Hi,
    Use the Constructor method to initialize the variable:
    WebClient := WebClient.WebClient;
    

    Some classes in .NET are static and can be used without construction, but most need to be initialized.

    Thank you!

    I have initialize the WebCLient, and now i see the next error:
    t295dp9on0km.png
    Access denied to 'c:/windows/system32/1001.pdf'..
    I deduce that the method, downloads by default to that directory,. is it possible to change that?

    Really appreciate your help!
  • DolshaDolsha Member Posts: 41
    AitorEG wrote: »
    vaprog wrote: »
    Hi,
    Use the Constructor method to initialize the variable:
    WebClient := WebClient.WebClient;
    

    Some classes in .NET are static and can be used without construction, but most need to be initialized.

    Thank you!

    I have initialize the WebCLient, and now i see the next error:
    t295dp9on0km.png
    Access denied to 'c:/windows/system32/1001.pdf'..
    I deduce that the method, downloads by default to that directory,. is it possible to change that?

    Really appreciate your help!

    Try
    FilePath := 'C:\Users\UserName\Desktop\1010.pdf';
    WebClient.DownloadFile(URL,FilePath);
  • AitorEGAitorEG Member Posts: 342
    Dolsha wrote: »
    AitorEG wrote: »
    vaprog wrote: »
    Hi,
    Use the Constructor method to initialize the variable:
    WebClient := WebClient.WebClient;
    

    Some classes in .NET are static and can be used without construction, but most need to be initialized.

    Thank you!

    I have initialize the WebCLient, and now i see the next error:
    t295dp9on0km.png
    Access denied to 'c:/windows/system32/1001.pdf'..
    I deduce that the method, downloads by default to that directory,. is it possible to change that?

    Really appreciate your help!

    Try
    FilePath := 'C:\Users\UserName\Desktop\1010.pdf';
    WebClient.DownloadFile(URL,FilePath);

    Really thanks, it worked!!!!
  • DolshaDolsha Member Posts: 41
    AitorEG wrote: »
    Dolsha wrote: »
    AitorEG wrote: »
    vaprog wrote: »
    Hi,
    Use the Constructor method to initialize the variable:
    WebClient := WebClient.WebClient;
    

    Some classes in .NET are static and can be used without construction, but most need to be initialized.

    Thank you!

    I have initialize the WebCLient, and now i see the next error:
    t295dp9on0km.png
    Access denied to 'c:/windows/system32/1001.pdf'..
    I deduce that the method, downloads by default to that directory,. is it possible to change that?

    Really appreciate your help!

    Try
    FilePath := 'C:\Users\UserName\Desktop\1010.pdf';
    WebClient.DownloadFile(URL,FilePath);

    Really thanks, it worked!!!!

    Great :)
  • tomguantomguan Member Posts: 14
    Hi all experts, I am testing some file downloading from https URL, and found this post, but I could not find system.net.webclient from .net, only can find system.net. I am using NAV2017 build 16585. Am I missing anything?

    Thanks
  • DuikmeesterDuikmeester Member Posts: 307
    WebClient is found in the assembly System.
Sign In or Register to comment.