Stream stream = await new HttpClient().GetStreamAsync( url );
如何在应用程序的本地文件夹中创建一个文件,并将通过此流下载的数据作为文件内容?
Stream stream = await new HttpClient().GetStreamAsync( url );
如何在应用程序的本地文件夹中创建一个文件,并将通过此流下载的数据作为文件内容?
你可能会做某种流来流复制。但是,我恰好在我的代码中使用了响应对象:
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);
您需要使用StorageFile
像这样的东西:
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("yourfile.xml", CreationCollisionOption.ReplaceExisting);