Search code examples
cordovauwpcordova-win10

Cordova - read/write access to shared folder in Win10


At work we have an existing Cordova app that runs on Android. When importing/exporting data, we implement the following process:

  • Data is exported from our desktop app into a sqlite database
  • The sqlite database is copied from the host PC to the Android device (using the sdcard)
  • The Cordova app is taken offline/onsite and is used to update the sqlite database
  • When the user has finished with the Cordova app (and returns to base) the sqlite file is copied back to the host
  • The desktop app is used to import updated data from the sqlite database

I'm currently porting the Cordova app to Windows UWP (specifically, Windows 10). Is there a way to achieve the above using the file system? Originally, I thought that it would be possible to copy the files to a folder underneath "Documents" and the app would be allowed to read/write this content.

However, when I inspect the object returned by window.requestFileSystem - it has 2 properties root and winpath. It looks like I only have read/write access to the app folder/sandbox.

Can this be done, or do I have to start looking at network-based options? Our clients prefer a file-based solution. Moving to a network-based solution for them will be undesirable.

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


Solution

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

    Yes, there is a way to upload/download files to a shared location on the device, but not with File Plugin. For the directories that file plugin can access, please refer to Where to Store Files.

    For Libraries like Music, Picture or Document, you can follow the below steps to access the folder:

    1. Create a custom plugin(cordova-plugin-capability-manager) to manage the capabilities on windows platform(below is the 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>
      

      Notes: If you want to use documentLibrary, you will need to declare fileTypeAssociation in windows manifest file as shown in the above codes. For details, please refer to documentLibrary's Prerequisites of KnownFolders.

    2. Add the plugin manually(Include the plugin folder into your project, add it in config.xml). And please do not add this plugin through VS config.xml designer as you will get xml parse error, and plugin won't be added successfully.:

      </widget>
           ...
          <plugin name="cordova-plugin-capability-manager" spec="~0.0.1"  />
      </widget>
      
    3. Use WinRT KnownFolders API directly in your cordova js file:

      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;
          });
      }