Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
FTP Upload
#1
Once again a little bit rusty.

Do you have a simple example for ftp upload with progress and a message when upload was successful?
#2
With progress no. Without - example in macro "Ftp help".
#3
Macro FTP upload with C#
Code:
Copy      Help
str file.expandpath("$qm$\qm.exe") url("ftp://ftp.server.com/public_html/test/") user("user") password("password")
int autoStart=1
int useCallback=1 ;;if 0, this example will auto-close the dialog when uploaded successfully

if useCallback
,CsFunc("", "q:\app\qm.exe", "ftp://ftp.quickmacros.com/public_html/test/", "quickmac", "jucabA8~/ke+f4", autoStart, 0, &sub.OnFinished)
else
,int success=CsFunc("", "q:\app\qm.exe", "ftp://ftp.quickmacros.com/public_html/test/", "quickmac", "jucabA8~/ke+f4", autoStart, 1, 0)
,out iif(success "uploaded" "failed")


#sub OnFinished ;;callback function
function# success
out iif(success "uploaded" "failed")


#ret
//C# code
using System;
using System.Drawing;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

public class Test
{
,public delegate void CbType(bool success);
,
,public static bool TestFtpUploadWithProgress(string localFile, string ftpURL, string ftpUser, string ftpPassword, bool autoStart, bool autoClose, int cbFunc)
,{
,,var f = new FtpUploadForm();

,,f.localFile = localFile;
,,f.ftpURL = ftpURL;
,,f.ftpUser = ftpUser;
,,f.ftpPassword = ftpPassword;
,,if(cbFunc!=0) f.cbFunc = (CbType)Marshal.GetDelegateForFunctionPointer((IntPtr)cbFunc, typeof(CbType));
,,f.autoStart = autoStart;
,,f.autoClose = autoClose;
,,
,,f.ShowDialog();
,,
,,return f.success;
,}

,class FtpUploadForm :Form
,{
,,public string localFile { get; set; }
,,public string ftpURL { get; set; }
,,public string ftpUser { get; set; }
,,public string ftpPassword { get; set; }
,,public CbType cbFunc { get; set; }
,,public bool autoStart { get; set; }
,,public bool autoClose { get; set; }
,,public bool success { get; private set; }

,,Button button1;
,,Label label1;
,,Label label2;
,,ProgressBar progressBar1;

,,public FtpUploadForm()
,,{
,,,var size = new Size(400, 240);

,,,button1 = new Button();
,,,button1.Bounds = new Rectangle(10, 10, 50, 24);
,,,button1.Text = "Upload";
,,,button1.Click += button1_Click;

,,,label1 = new Label();
,,,label1.Bounds = new Rectangle(10, 50, size.Width - 20, 50);

,,,label2 = new Label();
,,,label2.Bounds = new Rectangle(10, 100, size.Width - 20, 100);

,,,progressBar1 = new ProgressBar();
,,,progressBar1.Maximum = 100;
,,,progressBar1.Bounds = new Rectangle(10, 200, size.Width - 20, 20);

,,,this.Controls.Add(button1);
,,,this.Controls.Add(label1);
,,,this.Controls.Add(label2);
,,,this.Controls.Add(progressBar1);

,,,this.Text = "FTP Upload";
,,,this.ClientSize = size;
,,,this.StartPosition = FormStartPosition.CenterScreen;
,,}

,,protected override void OnLoad(EventArgs e)
,,{
,,,if(ftpURL.EndsWith("/")) ftpURL += Path.GetFileName(localFile);
,,,label1.Text = localFile + "\n" + ftpURL;
,,,base.OnLoad(e);
,,,
,,,if(autoStart) {
,,,,var t = new Timer(); t.Interval = 1; t.Tick += (unu, sed) => { t.Stop(); button1_Click(null, null); }; t.Start();
,,,}
,,}

,,private async void button1_Click(object sender, EventArgs e)
,,{
,,,await processFtp();
,,,if(cbFunc!=null) cbFunc(this.success);
,,,if(autoClose && this.success) this.Close();
,,}

,,async Task processFtp()
,,{
,,,label2.Text = "Uploading";
,,,button1.Enabled = false;
,,,this.success = false;
,,,string result = "";
,,,try {
,,,,FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpURL);
,,,,request.Method = WebRequestMethods.Ftp.UploadFile;
,,,,request.Credentials = new NetworkCredential(ftpUser, ftpPassword);

,,,,request.KeepAlive = false;
,,,,request.ReadWriteTimeout = 1000000000;
,,,,request.Timeout = 1000000000;

,,,,using(FileStream fs = File.OpenRead(localFile)) {
,,,,,using(Stream requestStream = request.GetRequestStream()) {
,,,,,,byte[] b = new byte[10 * 1024];
,,,,,,int readLength = 0;
,,,,,,int sentLength = 0;
,,,,,,while((readLength = fs.Read(b, 0, b.Length)) > 0) {
,,,,,,,await requestStream.WriteAsync(b, 0, b.Length);
,,,,,,,int percentComplete = (int)((float)(sentLength += readLength) / (float)fs.Length * 100);
,,,,,,,progressBar1.Value = percentComplete;
,,,,,,}
,,,,,}
,,,,}

,,,,FtpWebResponse response = (FtpWebResponse)request.GetResponse();
,,,,result = "Uploaded\nStatus: " + response.StatusDescription;
,,,,response.Close();
,,,,this.success = true;
,,,}
,,,catch(WebException e) {
,,,,result = "Failed\n" + e.Message;
,,,,if(e.Status == WebExceptionStatus.ProtocolError) {
,,,,,result = result + "Status Code : " + ((FtpWebResponse)e.Response).StatusCode;
,,,,,result = result + "\nStatus Description : " + ((FtpWebResponse)e.Response).StatusDescription;
,,,,}
,,,}
,,,catch(Exception e) {
,,,,result = e.Message;
,,,}
,,,label2.Text = result;
,,,button1.Enabled = true;
,,}
,}
}
#4
Wow, may thanks!!!
This is quite fancy.

await is quite new for me.
https://docs.microsoft.com/en-us/dotnet ... ords/await
#5
Where i do ihave to put await processFtp(); in the script, so that the upload starts automatically?
#6
Add this at the end of OnLoad:

var t = new Timer(); t.Interval = 1; t.Tick += (unu, sed) => { t.Stop(); button1_Click(null, null); }; t.Start();
#7
Thanks for yur help!

Where invoke the positive upload feedback, so that in can do my stuff?
When Upload went wrong, thit is not a problem.
#8
Code updated. Added parameters for auto-start, auto-close and on-finished callback function.


Forum Jump:


Users browsing this thread: 1 Guest(s)