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
No PM,please use the forum. || May the <SOLVED>-attribute be in your title!
Blog - rockwithnav.wordpress.com/
Twitter - https://twitter.com/RockwithNav
Facebook - https://facebook.com/rockwithnav/
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!
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!
Duikmeester recomended you a .NET class. You can use in in C/AL by declaring a variable of type DotNet and subtype
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:
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,
When I run the codeUnit, I see the next error:
What I'm doing wrong?
Use the Constructor method to initialize the variable:
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:
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
Thanks