0
I started using a MEGA file cloud API where I can upload and download the files from the cloud. Nuget Package here, I’m doing fine:
using CG.Web.MegaApiClient;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
namespace WindowsFormsMegaDownload
{
public partial class Form1 : Form
{
private double bytesTotal;
private long bytesT;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
var client = new MegaApiClient();
client.Login("Meu Email", "Senha");
//INode principal
IEnumerable<INode> home = client.GetNodes();
List<INode> homeContent = home.Where(n => n.Type == NodeType.Directory).ToList();
INode home_jogos = homeContent.FirstOrDefault(f => f.Name == "Jogos");
//Node da home\\pasta
IEnumerable<INode> jogos = client.GetNodes(home_jogos);
List<INode> allfiles = jogos.Where(n => n.Type == NodeType.File).ToList();
INode myFile = allfiles.FirstOrDefault(f => f.Name == "sparkle_setup.exe");
bytesTotal = double.Parse(myFile.Size.ToString()) / 1024 / 1024;
bytesT = myFile.Size;
var progress = new Progress<double>();
progress.ProgressChanged += new EventHandler<double>(progressChanged);
Thread progressCompleted = new Thread(new ThreadStart(progressComplete));
progressCompleted.Start();
client.DownloadFileAsync(myFile, "sparkle_setup.exe", progress);
}
private void progressComplete()
{
bool completed = false;
while (!completed)
{
if (File.Exists("sparkle_setup.exe"))
{
if(new FileInfo("sparkle_setup.exe").Length == bytesT)
{
completed = true;
}
}
}
}
private void progressChanged(object sender, double e)
{
double bytesIn = double.Parse(new FileInfo("sparkle_setup.exe").Length.ToString()) / 1024 / 1024;
progressBar1.Value = Convert.ToInt32(e);
label1.Text = progressBar1.Value.ToString() + "% | " + bytesIn.ToString("F2") + "/" + bytesTotal.ToString("F2") + " MB";
}
}
}
In JIT:
First start with logging in, then create inodes that are the path to the cloud folders until you get what you need. Like Megaapiclient, the class is not similar to Webclient about the events I decided to do manually. Method Downloadfileasync.
public Task DownloadFileAsync(INode node, string outputFile, IProgress<double> progress = null, CancellationToken? cancellationToken = default(CancellationToken? ))
This is the structure of the method, I put the Inode of the file I had (I didn’t even remember it), the output path and a Progress item that makes the progressiChanged. I just don’t get this CancellationToken? cancellationToken = default(CancellationToken? )
. Does he have anything to do with whether or not progress has been completed?
I didn’t do anything about this parameter instead I created a Downloadcompleted thread.