这是我下载文件的代码。 但我不知道如何将文件写入磁盘。
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);
}
我不知道在这里写什么以及在哪里调用WriteToFile():
private async Task WriteToFile()
{
var myFile = await KnownFolders.MusicLibrary.CreateFileAsync(filename.Replace("---", " - ")+".mp3", CreationCollisionOption.GenerateUniqueName);
///stuck here
}
答案 0 :(得分:2)
我这样做了:
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 { }
}
并写入磁盘:
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);
}