Windows 10应用程序 - 文件下载

时间:2015-09-17 11:17:46

标签: winjs windows-10

我正在使用Windows 10通用应用程序,我想下载一个文件。到Sharepoint服务器的文件链接。我已将headr中的令牌传递给Web服务,然后将返回的字节数组服务到我的WinJS。

现在我要保存文件,我该怎么做?我尝试了几个代码示例但没有工作。

var folder = Windows.Storage.ApplicationData.current.localFolder;
folder.createFileAsync("document.docx", Windows.Storage.CreationCollisionOption.replaceExisting).then(function (file) {
    return Windows.Storage.FileIO.writeTextAsync(file, result.response);
}).then(function () {
    //saved
});

我正在使用上面的代码,它正在创建新文件,但没有放置内容。请建议做什么。

1 个答案:

答案 0 :(得分:0)

您永远不会为WriteAccess打开文件。我已经在我的工作应用中添加了代码。首先执行此命令

 StorageFile ageFile = await local.CreateFileAsync("Age.txt", CreationCollisionOption.FailIfExists);

然后这样做:

// Get the file. 
var ageFile = await local.OpenStreamForWriteAsync("Age.txt",CreationCollisionOption.OpenIfExists); 
// Read the data. 
using (StreamWriter streamWriter = new StreamWriter(ageFile)) 
{ 
  streamWriter.WriteLine(cmbAgeGroup.SelectedIndex + ";" + DateTime.Now); 
  streamWriter.Flush(); 
} 
ageFile.Dispose();