下载并保存HttpRequestMessage C#

时间:2014-11-14 16:34:56

标签: c# windows-phone-8

在我的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);

如果我运行调试,在response.content中我有下载。现在,我想将它保存到隔离存储中。我怎样才能做到这一点 ?我应该使用流吗?

由于

1 个答案:

答案 0 :(得分:0)

将您的回复转换为流并将其写入文件

using (var fileStream = await this.storageFile.OpenFileAsync(fileName, FileMode.OpenOrCreate))
{      
    await stream.CopyToAsync(fileStream);
}