Permalink
Browse files

Added a simple FTP file uploader

  • Loading branch information...
1 parent d8a6f48 commit 658c93d19f919c28827637fbf782c31e335f5091 @JeroenVandezande committed Aug 9, 2012
Showing with 49 additions and 0 deletions.
  1. +48 −0 RemObjects.Train/API/FTP.pas
  2. +1 −0 RemObjects.Train/RemObjects.Train.oxygene
@@ -0,0 +1,48 @@
+namespace RemObjects.Train.API;
+
+interface
+
+uses
+ RemObjects.Train,
+ RemObjects.Script.EcmaScript,
+ RemObjects.Script.EcmaScript.Internal,
+ System.Text,
+ System.Net,
+ System.IO;
+
+type
+
+ [PluginRegistration]
+ FTPPlugIn = public class(IPluginRegistration)
+ private
+ protected
+ public
+ method &Register(aServices: IApiRegistrationServices);
+ [WrapAs('ftp.upload', SkipDryRun := true)]
+ class method FtpUpload(aServices: IApiRegistrationServices;ec: RemObjects.Script.EcmaScript.ExecutionContext; aServer, aUsername, aPassword, aFileName, aRemote: String);
+ end;
+
+implementation
+
+method FTPPlugIn.&Register(aServices: IApiRegistrationServices);
+begin
+ var rov := aServices.RegisterObjectValue('ftp');
+ rov.AddValue('upload', RemObjects.Train.MUtilities.SimpleFunction(aServices.Engine, typeOf(FTPPlugIn), 'FtpUpload'));
+end;
+
+class method FTPPlugIn.FtpUpload(aServices: IApiRegistrationServices; ec: RemObjects.Script.EcmaScript.ExecutionContext; aServer, aUsername, aPassword, aFileName, aRemote: String);
+begin
+ aFileName := aServices.ResolveWithBase(ec, aFileName);
+ using client := new WebClient() do
+ begin
+ client.Credentials := new NetworkCredential(aUsername, aPassword);
+ using sr := new System.IO.FileStream(aFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read) do
+ begin
+ var ba := new Byte[sr.Length];
+ sr.Read(ba, 0, ba.Length - 1);
+ client.UploadData(aServer + '/' + aRemote, ba);
+ end;
+ end;
+end;
+
+end.
@@ -84,6 +84,7 @@
<Compile Include="API\Delphi.pas" />
<Compile Include="API\Environment.pas" />
<Compile Include="API\File.pas" />
+ <Compile Include="API\FTP.pas" />
<Compile Include="API\Gac.pas" />
<Compile Include="API\Images.pas" />
<Compile Include="API\Ini.pas" />

0 comments on commit 658c93d

Please sign in to comment.