FTP upload with DotNet

ivarskivarsk Member Posts: 12
edited 2013-06-24 in NAV Three Tier
Hi, everyone.

I have created an FTP upload function from within NAV using .NET, so that no external libs are needed for that.
Gladly sharing this with you (not optimized or anything, but works):
URL := 'ftp://remote_server/remote_folder/testfile.txt';
FTPRequest := WebRequest.Create(URL);
Credentials := Credentials.NetworkCredential('some_login', 'some_password');
FTPRequest.Credentials := Credentials;
FTPRequest.KeepAlive := false;
FTPRequest.UseBinary := true;
FTPRequest.Method := 'STOR';
FTPRequest.UsePassive := true;
FileStream := FileA.OpenRead('c:\testfile.txt');
FTPRequest.GetRequestStream.Write(FileA.ReadAllBytes('c:\testfile.txt'), 0, FileStream.Length);

---

WebRequest	DotNet	'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.WebRequest	
FTPRequest	DotNet	'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.FtpWebRequest	
Credentials	DotNet	'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.NetworkCredential	
FileStream	DotNet	'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.FileStream	
FileA	DotNet	'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.File	
URL	Text		250

Now a question - I can't get my head around this:
The
FTPRequest.Method := 'STOR';
was my lucky guess, and every code sample I've seen actually says:
FTPRequest.Method := WebRequestMethods.Ftp.UploadFile;

As far as I understand, WebRequestMethods.Ftp is a .NET enumeration that in this case corresponds Strings.
How to use these enumerations appropriately?

Disclaimer: This is my first encounter with NAV+.NET :)

Comments

  • ivarskivarsk Member Posts: 12
    =D> Thank you, exactly what I was looking for.
  • asmilie2basmilie2b Member Posts: 122
    Hello Ivarsk,

    I wonder if you ever got this working with downloading by ftp?
    Thanks to your sharing I was able to get the upload working, but I can't figure out the syntax to make it download.
    My attempts shown here:
    viewtopic.php?f=32&t=58060

    Would really appreciate any advice. This is absolutely a first attempt with anything dotnet for me.

    Bruce Anderson
  • ivarskivarsk Member Posts: 12
    For the sake of having it all in one thread - an FTP download example:
    URL := 'ftp://remote_server/remote_folder/testfile.txt';
    FTPRequest := WebRequest.Create(URL);
    Credentials := Credentials.NetworkCredential('some_login', 'some_password');
    FTPRequest.Credentials := Credentials;
    FTPRequest.KeepAlive := FALSE;
    FTPRequest.UseBinary := TRUE;
    FTPRequest.Method := 'RETR';
    FTPRequest.UsePassive := TRUE;
    FTPResponse := FTPRequest.GetResponse();
    ResponseStream := FTPResponse.GetResponseStream();
    FileStream := FileA.Create('c:\testfile.txt');
    COPYSTREAM(FileStream, ResponseStream);
    FileStream.Close;
    
    ---
    
    WebRequest	DotNet	System.Net.WebRequest.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
    FTPRequest	DotNet	System.Net.FtpWebRequest.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
    Credentials	DotNet	System.Net.NetworkCredential.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
    FTPResponse	DotNet	System.Net.FtpWebResponse.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
    ResponseStream	InStream		
    FileStream	DotNet	System.IO.FileStream.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
    FileA	DotNet	System.IO.File.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
    

    Enjoy.
  • madsmorremadsmorre Member Posts: 40
    I'm trying to use this for upload, but receive the following error - any ideas?
    01xtcr0l88mf.png

    Best Regards
    Mads Morre
Sign In or Register to comment.