Try replacing File.Create with:
using var stream = new FileStream( filePath, FileMode.Append );
The files must be deleted before starting the operations.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Good morning all,
so i have been trying now for a couple of days to make a simple downloader,
seen a couple of text tutorials but to no avail
what i am doing is checking if say python for example is installed then check if c++ framwork installed,
if they are not
i want to create a downloader that is a pop-up that i have managed to do the ui,
but setting it up is driving me crazy i have tried net.http web client
and none of these are giving me downloading with progress
i followed this to a tee
that has worked i got it to process downloads however getting them to save has failed
private async void DoWork(object sender, DoWorkEventArgs e)
{
if (CurrentProgress >= 100)
{
CurrentProgress = 0;
}
TotalPercentage = CurrentProgress + " %" + " downloaded in total";
var total = _urls.Count;
var count = 0;
foreach (var url in _urls)
{
// Do the download
DownloadInfo = "Downloading: " + url;
var s = url;
var last = s.Split('/').Last();
await _model.HttpGetForLargeFile(url, last, new CancellationTokenSource().Token);
CurrentProgress = 100 / (total - count);
TotalPercentage = CurrentProgress + " %" + " downloaded in total";
count++;
}
}
however when i get to saving this is what i have
using (var stream = await response.Content.ReadAsStreamAsync())
{
var totalRead = 0L;
var buffer = new byte[4096];
var moreToRead = true;
do
{
token.ThrowIfCancellationRequested();
var read = await stream.ReadAsync(buffer, 0, buffer.Length, token);
if (read == 0)
{
moreToRead = false;
}
else
{
var data = new byte[read];
buffer.ToList().CopyTo(0, data, 0, read);
// TODO: write the actual file to disk
SaveByteArrayToFileWithFileStream(data, "C:\\Temp\\" + filename);
// Update the percentage of file downloaded
totalRead += read;
if (canReportProgress)
{
var downloadPercentage = ((totalRead * 1d) / (total * 1d)) * 100;
var value = Convert.ToInt32(downloadPercentage);
PercentageChanged.Raise(this, (value.ToString()));
}
}
}
while (moreToRead);
}
}
}
}
public static void SaveByteArrayToFileWithFileStream(byte[] data, string filePath)
{
using var stream = File.Create(filePath);
stream.Write(data, 0, data.Length);
}
by default did not come with file name so plugged that in and then a simple file create from the bytes but the file it creates is all invalid and dont open up
public MainWindowViewModel()
{
_model = new MainWindowModel();
_model.PercentageChanged += OnPercentageChanged;
_worker.DoWork += DoWork;
_worker.ProgressChanged += ProgressChanged;
_worker.WorkerReportsProgress = true;
_worker.WorkerSupportsCancellation = true;
CurrentProgress = 0;
_urls = new List<string>
{
"http://212.183.159.230/20MB.zip",
"http://212.183.159.230/5MB.zip",
};
}
urls are just dummy files that are provided in the tut
when i can get it working correctly i will generate a list of download links depending if those files are needed to be downloaded
any help would be much appreciated
Try replacing File.Create with:
using var stream = new FileStream( filePath, FileMode.Append );
The files must be deleted before starting the operations.