javascript - 如何处理 Windows 应用商店/WinJS 应用程序中的文件激活

标签 javascript windows-store-apps winjs

我正在尝试让我的文本编辑器应用程序处理文件启动。 Microsoft 在此处提供了如何执行此操作的示例:

http://msdn.microsoft.com/en-us/library/windows/apps/hh452684.aspx

不幸的是,它在接收文件时停止,并且没有提供有关如何实际打开该文件的任何信息。

我可以成功处理激活的事件,并且最终得到文件的绝对路径。例如,

C:\Users\Rory\Documents\test.txt

Metro 应用无权访问绝对路径,除非在某些条件下。

  1. 如果用户通过文件选择器选择文件
  2. 如果应用之前访问过该文件并且路径已存储在 Windows.Storage.AccessCache 中
  3. 如果应用程序在启动时传递该文件。

即使在这种情况下适用第 3 条,我也无法打开该文件。

我尝试过 Windows.Storage.StorageFile.getFileFromPathAsync(path_to_file) 但收到此错误

0x80070005 - JavaScript runtime error: Access is denied.

WinRT information: Cannot access the specified file or folder (඀6). 
The item is not in a location that the application has access to (including 
application data folders, folders that are accessible via capabilities 
and persisted items in the StorageApplicationPermissions lists). Verify 
that the file is not marked with system or hidden file attributes.

我已将应用程序包 list 设置为接受 txt 文件。

最佳答案

StorageFileStorageFileWebUIFileActivatedEventArgs 参数中传递到您的应用。试试这个:

app.onactivated = function (args) {
    if (args.detail.kind === activation.ActivationKind.file) {
        if (args.detail.files.size > 0) {
            var storageFile = args.detail.files[0];
            Windows.Storage.FileIO.readTextAsync(storageFile).then(function (text) {
                // Do something with the content of the file.
            });
        }
    }

    // ...
} 

关于javascript - 如何处理 Windows 应用商店/WinJS 应用程序中的文件激活,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19194936/

相关文章:

javascript - 什么 VS 魔术提供 ".js.bundle"功能来组合 JavaScript 文件?

javascript - angularjs 的引用对象

c# - 将 cookie 设置为 WebView 控件

javascript - 如何使用 facebook javascript sdk 在页面粉丝选项卡内获取 facebook 页面 ID

javascript - 如何知道 HTMLTableDataCellElement 的整个文本被选中

c# - 语言选择 : C++ or C# for Windows Store app?

c# - Windows 应用商店应用程序的 Socket.ConnectAsync 不喜欢 Async

javascript - 由于搜索尚未运行和激活时如何调试应用程序

css - 如何在 Windows 8 Store 应用程序的不同设备上使用屏幕区域的所有高度设置自动列数?

security - 如何在 Windows 应用商店应用程序包中存储 "secret"字符串