下载并保存 HttpRequestMessage C#
本文关键字:HttpRequestMessage 保存 下载 | 更新日期: 2023-09-27 18:30:29
在我的WP8.1 Silverlight应用程序中,我想下载一个文件并将其保存到独立存储中。下载是可以的,但是我该如何保存它?
对于下载,我使用这个:
var HttpClientDownloadFile = new Windows.Web.Http.HttpClient();
Windows.Web.Http.HttpRequestMessage request = new Windows.Web.Http.HttpRequestMessage(Windows.Web.Http.HttpMethod.Get, new Uri(downloadUrl));
request.Headers.Add("Range", "bytes=0-");
// Hook up progress handler.
Progress<HttpProgress> progressCallback = new Progress<HttpProgress>(OnSendRequestProgress);
var tokenSource = new CancellationTokenSource();
Windows.Web.Http.HttpResponseMessage response = await HttpClientDownloadFile.SendRequestAsync(request).AsTask(tokenSource.Token, progressCallback);
如果我运行调试,在响应中,我有我的下载。现在,我想将其保存到独立存储中。我该怎么做?我应该使用流吗?
谢谢
将您的响应转换为流并将其写入文件
using (var fileStream = await this.storageFile.OpenFileAsync(fileName, FileMode.OpenOrCreate))
{
await stream.CopyToAsync(fileStream);
}