this is my code for downloading a file. but im not sure how to write the file to disk.
private async Task DownloadFile()
{
HttpStreamContent streamContent = new HttpStreamContent(new SlowInputStream(streamLength));
IProgress<HttpProgress> progress = new Progress<HttpProgress>(ShowProgress);
response = await httpClient.PostAsync(new Uri(downloadUrl), streamContent).AsTask(cts.Token, progress);
}
im not sure what to write here and where to call WriteToFile():
private async Task WriteToFile()
{
var myFile = await KnownFolders.MusicLibrary.CreateFileAsync(filename.Replace("---", " - ")+".mp3", CreationCollisionOption.GenerateUniqueName);
///stuck here
}
I got it working this way:
private async Task DownloadFile()
{
IProgress<HttpProgress> progress = new Progress<HttpProgress>(ShowProgress);
try
{
response = await httpClient.GetAsync(new Uri(downloadUrl), HttpCompletionOption.ResponseContentRead).AsTask(cts.Token, progress);
DownloadMessageText.Text = "Download complete. Writing to disk...";
await WriteToFile();
DownloadMessageText.Text = "File saved in music library.";
}
catch { }
}
and writing to disk:
private async Task WriteToFile()
{
var myFile = await KnownFolders.MusicLibrary.CreateFileAsync(filename.Replace("---", " - ") + ".mp3", CreationCollisionOption.GenerateUniqueName);
var fs = await myFile.OpenAsync(FileAccessMode.ReadWrite);
IBuffer buffer = await response.Content.ReadAsBufferAsync();
await fs.WriteAsync(buffer);
}