Download File


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

Ports
Text Type DataType Accepts IsList CustomAcception
Download 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
Received OUT Int64 MULTIPLE FALSE FALSE
ToReceive OUT Int64 MULTIPLE FALSE FALSE
Error OUT String MULTIPLE FALSE FALSE

Source
namespace Korduene.KGraph.Nodes.Net
{
    [IsNode("Download File")]
    public class FileDownload : Node
    {
        Port run = new Port(PortType.IN, typeof(RUN), AcceptsLinks.MULTIPLE,  "Download");
        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 received = new Port(PortType.OUT, typeof(long), AcceptsLinks.MULTIPLE, "Received");
        Port toreceive = new Port(PortType.OUT, typeof(long), AcceptsLinks.MULTIPLE, "ToReceive");
        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 receivedVar = new Variable("long", "", "0");
        Variable toreceiveVar = new Variable("long", "", "0");
        Variable errorVar = new Variable("string", "", "0");
        public FileDownload()
        {
            NodeType = KGraph.NodeType.Method;
            this.Name = "Download File";
            AddPort(run);
            AddPort(url);
            AddPort(path);
            AddPort(prgbar);
            AddPort(prgchanged);
            AddPort(complete);
            AddPort(prgrs);
            AddPort(received);
            AddPort(toreceive);
            AddPort(err);
            prgrsVar.Name = prgrs.ID;
            Variables.Add(prgrsVar);
            Variables.Add(receivedVar);
            Variables.Add(toreceiveVar);
            Variables.Add(errorVar);
            received.Instructions = "init";
            toreceive.Instructions = "init";
            prgrs.Instructions = "init";
            err.Instructions = "init";
            Information = "Downloads file from the specified URL, you can attach a progress bar to see download progress.";
        }
        public override string Code()
        {
            string filepath = string.Empty;
            string fileurl = string.Empty;
            prgrsVar.Name = prgrs.ID;
            receivedVar.Name = received.ID;
            toreceiveVar.Name = toreceive.ID;
            errorVar.Name = err.ID;
            errorVar.Initialize = " string.Empty";
            received.Instructions = receivedVar.Name;
            toreceive.Instructions = toreceiveVar.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.DownloadProgressChanged += delegate(object sender, System.Net.DownloadProgressChangedEventArgs e)" + Environment.NewLine +
            "{" + Environment.NewLine +
                "if (prgBar !=null){ prgBar.Value = e.ProgressPercentage; }" + Environment.NewLine +
                prgrs.ID + " = e.ProgressPercentage;" + Environment.NewLine +
                received.ID + " = e.BytesReceived;" + Environment.NewLine +
                toreceive.ID + " = e.TotalBytesToReceive;" + Environment.NewLine +
                GetValue(prgchanged) + 
            "};" + Environment.NewLine +
            "webClient.DownloadFileCompleted += delegate(object sender, System.ComponentModel.AsyncCompletedEventArgs e)" + Environment.NewLine +
            "{\r\n" + "if(e.Error !=null){ " + err.ID + " = e.Error.Message; }" + 
            GetValue(complete) + "\r\n};" + Environment.NewLine +
            "webClient.DownloadFileTaskAsync(new Uri(url), path);" ;
            if (prgbar.ConnectedPorts.Count >0)
            {
                code = code.Replace("//prgassign", "prgBar =" + GetValue(prgbar) + ";");
            }
            return code;
        }
        public override void UpdateResults()
        {
            Code();
        }
    }
}