Cordova - read/write 访问 Win10 中的共享文件夹

Cordova - read/write access to shared folder in Win10

在工作中,我们有一个在 Android 上运行的现有 Cordova 应用程序。当有importing/exporting条数据时,我们执行以下流程:

我目前正在将 Cordova 应用程序移植到 Windows UWP(具体来说,Windows 10)。有没有办法使用文件系统实现上述目标?最初,我认为可以将文件复制到 "Documents" 下的文件夹,然后应用程序将被允许 read/write 此内容。

但是,当我检查 window.requestFileSystem 返回的对象时 - 它有 2 个属性 rootwinpath。我似乎只有 read/write 访问应用 folder/sandbox.

这可以做到吗,还是我必须开始考虑基于网络的选项?我们的客户更喜欢基于文件的解决方案。转向基于网络的解决方案对他们来说是不可取的。

有没有办法将文件 upload/download 保存到设备上的共享位置?

Is there a way to upload/download files to a shared location on the device?

是的,有一种方法可以将 upload/download 文件保存到设备上的共享位置,但不能使用 File Plugin。文件插件可以访问的目录,请参考Where to Store Files.

对于音乐、图片或文档等库,您可以按照以下步骤访问文件夹:

  1. 创建一个自定义插件(cordova-plugin-capability-manager)来管理windows平台上的功能(下面是plugin.xml):

    <?xml version="1.0" encoding="UTF-8"?>
    <plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
            id="cordova-plugin-capability-manager" version="0.0.1">
        <name>Capability Manager</name>
        <description>Cordova Capability Manager Plugin</description>
        <license>Apache 2.0</license>
        <keywords>cordova,device</keywords>
        <platform name="windows">
            <config-file target="package.appxmanifest" parent="/Package/Capabilities">
                <uap:Capability Name="documentsLibrary" />
                <uap:Capability Name="picturesLibrary" />
                <uap:Capability Name="musicLibrary" />
            </config-file>
            <config-file target="package.appxmanifest" parent="/Package/Applications/Application/Extensions" >
                    <uap:Extension Category="windows.fileTypeAssociation">
                        <uap:FileTypeAssociation Name="text">
                            <uap:SupportedFileTypes>
                                <uap:FileType>.txt</uap:FileType>
                            </uap:SupportedFileTypes>
                        </uap:FileTypeAssociation>
                    </uap:Extension>
            </config-file>
        </platform>
    </plugin>
    

    注意:如果你想使用documentLibrary,你需要在windows清单文件中声明fileTypeAssociation,如上面的代码所示。详情请参考documentLibrary's Prerequisites of KnownFolders.

  2. 手动添加插件(将插件文件夹包含到您的项目中,在config.xml中添加)。 请不要通过 VS config.xml 设计器添加此插件,因为您将得到 xml 解析错误,并且插件将无法添加成功。:

    </widget>
         ...
        <plugin name="cordova-plugin-capability-manager" spec="~0.0.1"  />
    </widget>
    
  3. 直接在你的 cordova js 文件中使用 WinRT KnownFolders API:

    if (cordova.platformId === "windows")
    {
     Windows.Storage.KnownFolders.documentsLibrary
        .createFileAsync("abc.txt", Windows.Storage.CreationCollisionOption.replaceExisting)
        .then(function (file) {
            var abc = file;
        }, function (error) {
            var cba = error;
        });
    }