1
Stream stream = await new HttpClient().GetStreamAsync( url );

如何在应用程序的本地文件夹中创建一个文件,并将通过此流下载的数据作为文件内容?

4

2 回答 2

2

你可能会做某种流来流复制。但是,我恰好在我的代码中使用了响应对象:

var client = new HttpClient();
var response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
    Stream stream = null;
    StorageFolder localFolder = ApplicationData.Current.TemporaryFolder;
    StorageFile file = await localFolder.CreateFileAsync("savename.htm",
        CreationCollisionOption.ReplaceExisting);
    stream = await file.OpenStreamForWriteAsync();

    await response.Content.CopyToAsync(stream);
于 2013-03-24T18:17:11.510 回答
0

您需要使用StorageFile

像这样的东西:

StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("yourfile.xml", CreationCollisionOption.ReplaceExisting);

更多信息,请访问 http://social.msdn.microsoft.com

于 2013-03-24T14:47:56.833 回答