Upload File


Node Information
Uploads file to the specified URL, you can attach a progress bar to see upload progress.
General Information
• This node doesn't have other available options.
Visual
Visual

Ports
Text Type DataType Accepts IsList CustomAcception
Upload IN RUN MULTIPLE FALSE FALSE
URL IN String ONE FALSE FALSE
Path IN String ONE FALSE FALSE
ProgressBar IN ProgressBar ONE FALSE FALSE
Changed OUT RUN MULTIPLE FALSE FALSE
Complete OUT RUN MULTIPLE FALSE FALSE
Progress OUT Int32 MULTIPLE FALSE FALSE
Sent OUT Int64 MULTIPLE FALSE FALSE
ToSend OUT Int64 MULTIPLE FALSE FALSE
Error OUT String MULTIPLE FALSE FALSE

Source
namespace Korduene.KGraph.Nodes.Net
{
    [IsNode("Upload File")]
    public class FileUpload : Node
    {
        Port run = new Port(PortType.IN, typeof(RUN), AcceptsLinks.MULTIPLE,  "Upload");
        Port complete = new Port(PortType.OUT, typeof(RUN), AcceptsLinks.MULTIPLE,  "Complete");
        Port err = new Port(PortType.OUT, typeof(string), AcceptsLinks.MULTIPLE, "Error");
        Port prgchanged = new Port(PortType.OUT, typeof(RUN), AcceptsLinks.MULTIPLE,  "Changed");
        Port prgrs = new Port(PortType.OUT, typeof(int), AcceptsLinks.MULTIPLE,  "Progress");
        Port sent = new Port(PortType.OUT, typeof(long), AcceptsLinks.MULTIPLE, "Sent");
        Port tosend = new Port(PortType.OUT, typeof(long), AcceptsLinks.MULTIPLE, "ToSend");
        Port url = new Port(PortType.IN, typeof(string), AcceptsLinks.ONE, "URL");
        Port path = new Port(PortType.IN, typeof(string), AcceptsLinks.ONE, "Path");
        Port prgbar = new Port(PortType.IN, typeof(Korduene.Designer.Standard.ProgressBar), AcceptsLinks.ONE,  "ProgressBar");
        public string URL
        {
            set { url.Value = value.ToString(); UpdateResults(); }
            get { return GetRuntimeValue(url); }
        }
        public string FilePath
        {
            set { path.Value = value.ToString(); }
            get { return GetRuntimeValue(path); }
        }
        Variable prgrsVar = new Variable("int", "", "0");
        Variable sentVar = new Variable("long", "", "0");
        Variable tosendVar = new Variable("long", "", "0");
        Variable errorVar = new Variable("string", "", "0");
        public FileUpload()
        {
            NodeType = KGraph.NodeType.Method;
            this.Name = "Upload File";
            AddPort(run);
            AddPort(url);
            AddPort(path);
            AddPort(prgbar);
            AddPort(prgchanged);
            AddPort(complete);
            AddPort(prgrs);
            AddPort(sent);
            AddPort(tosend);
            AddPort(err);
            prgrsVar.Name = prgrs.ID;
            Variables.Add(prgrsVar);
            Variables.Add(sentVar);
            Variables.Add(tosendVar);
            Variables.Add(errorVar);
            sent.Instructions = "init";
            tosend.Instructions = "init";
            prgrs.Instructions = "init";
            err.Instructions = "init";
            Information = "Uploads file to the specified URL, you can attach a progress bar to see upload progress.";
        }
        public override string Code()
        {
            string filepath = string.Empty;
            string fileurl = string.Empty;
            prgrsVar.Name = prgrs.ID;
            sentVar.Name = sent.ID;
            tosendVar.Name = tosend.ID;
            errorVar.Name = err.ID;
            errorVar.Initialize = " string.Empty";
            sent.Instructions = sentVar.Name;
            tosend.Instructions = tosendVar.Name;
            prgrs.Instructions = prgrsVar.Name;
            err.Instructions = errorVar.Name;
            if (url.IsConnected) { fileurl = GetValue(url); }
            else { fileurl = "@" + q + URL + q; }
            if (path.IsConnected) { filepath = GetValue(path); }
            else { filepath = "@" + q + FilePath + q; }
            string code =
            errorVar.Name + " = string.Empty;\r\n" +
            "System.Net.WebClient webClient = new System.Net.WebClient();" + Environment.NewLine +
            "string url = " + fileurl + ";" + Environment.NewLine +
            "string path = " + filepath + ";" + Environment.NewLine +
            "ProgressBar prgBar = null;" + Environment.NewLine +
            "//prgassign" + Environment.NewLine +
            "webClient.UploadProgressChanged += delegate(object sender, System.Net.UploadProgressChangedEventArgs e)" + Environment.NewLine +
            "{" + Environment.NewLine +
                "if (prgBar !=null){ prgBar.Value = e.ProgressPercentage; }" + Environment.NewLine +
                prgrs.ID + " = e.ProgressPercentage;" + Environment.NewLine +
                sent.ID + " = e.BytesSent;" + Environment.NewLine +
                tosend.ID + " = e.TotalBytesToReceive;" + Environment.NewLine +
                GetValue(prgchanged) + 
            "};" + Environment.NewLine +
            "webClient.UploadFileCompleted += delegate(object sender, System.Net.UploadFileCompletedEventArgs e)" + Environment.NewLine +
            "{\r\n" + "if(e.Error !=null){" + err.ID + " = e.Error.Message;}" + 
            GetValue(prgchanged) + "\r\n};" + Environment.NewLine +
            "webClient.UploadFileTaskAsync(new Uri(url), path);" + Environment.NewLine;
            code = code.Replace("completeCalls", GetValue(complete));
            if (prgbar.IsConnected)
            {
                code = code.Replace("//prgassign", "prgBar =" + GetValue(prgbar) + ";");
            }
            return code;
        }
        public override void UpdateResults()
        {
            Code();
        }
    }
}