title: Cordova Download File From App to Device
body: <p>I am working on a mobile app using Cordova. There it a tutorial in .docx format in the app and I need to pass it from the app to the device so it can be opened. Is there is a better way to do this let me know please but here is what I have. The user clicks a button to view the tutorial doc which then opens in whatever app the user has that can view .docx files. It works just fine on Android but not on iOS and I can't figure out why. A FileTransferError is thrown in the download function with error code 3. on iOS but not Android. I have searched around but everything I can find is for downloading from a web server. Can anyone tell me why I get an error code 3 on iOS but not Android?</p>
  
  <p>This is the code that is called when the button to view the tutorial is clicked:<br></p>
  
  <pre><code>function downloadTutorial() {
      var userAgent = navigator.userAgent || navigator.vendor || window.opera;
      if (userAgent.indexOf("Android") &gt; -1) {
          window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function (directoryEntry) {
              directoryEntry.getFile("app-tutorial.docx", { create: true }, function (fileEntry) {
  
              download(fileEntry, encodeURI(cordova.file.applicationDirectory + "www/docs/app-tutorial-en.docx"));
              }, function(e1){
                  console.error(`get file failed`);
                  console.log(e1);
              });
          }, function(e) {
              console.error(`write failed`);
              console.log(e);
          });
      }
      else if ((userAgent.indexOf("iPad") &gt; -1 || userAgent.indexOf("iPhone") &gt; -1 || userAgent.indexOf("iPod") &gt; -1) &amp;&amp; !window.MSStream) {
          window.resolveLocalFileSystemURL(cordova.file.documentsDirectory, function (directoryEntry) {
              console.log(directoryEntry);
              directoryEntry.getFile("app-tutorial.docx", { create: true }, function (fileEntry) {
                  console.log(fileEntry);
  
                  download(fileEntry, encodeURI(cordova.file.applicationDirectory + "www/docs/app-tutorial-en.docx"));
              }, function(e1){
                  console.error(`get file failed`);
                  console.log(e1);
              });
          }, function(e) {
              console.error(`write failed`);
              console.log(e);
          });
      }
  }
  </code></pre>
  
  <p>And here is the download function:</p>
  
  <pre><code>function download(fileEntry, uri) {
      var fileTransfer = new FileTransfer();
      var fileURL = fileEntry.toURL();
      console.log(fileURL);
      var options = new FileUploadOptions();
      options.headers = { Connection: "close" };
  
      fileTransfer.download(uri, encodeURI(fileURL), function (entry) {
          console.log("Successful downloaded tutorial file.");
  
          cordova.plugins.fileOpener2.open(
              fileURL,
              "application/vnd.openxmlformats-officedocument.wordprocessingml.document", {
                  error : function(e) {
                      console.error(`Error status: ${e.status} - Error message: ${e.message}`);
                  },
                  success : function () {
                      console.log('file opened successfully');
                  }
              }
          );
      },
      function (error) {
          console.error(`file transfer error ${error}`); // a FileTransferError is logged here with the source, destination and error code 3
      }, false, options);
  }
  </code></pre>
  
  <p>config.xml</p>
  
  <pre><code>&lt;widget id="" version="0.18.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0"&gt;
  &lt;name&gt;&lt;/name&gt;
  &lt;description&gt;
  
  &lt;/description&gt;
  &lt;author email="" href=""&gt;
  
  &lt;/author&gt;
  &lt;content src="index.html" /&gt;
  &lt;plugin name="cordova-plugin-whitelist" spec="1" /&gt;
  &lt;plugin name="cordova-plugin-console" spec="1.0.3" /&gt;
  &lt;plugin name="cordova-plugin-file" spec="4.2.0"/&gt;
  &lt;plugin name="cordova-plugin-email" spec="1.1.1"/&gt;
  &lt;plugin name="cordova-sms-plugin"/&gt;
  &lt;plugin name="cordova-plugin-vibration"/&gt;
  &lt;plugin name="cordova-plugin-market"/&gt;
  &lt;plugin name="cordova-plugin-ble-central"/&gt;
  &lt;plugin name="cordova-sqlite-storage"/&gt;
  &lt;plugin name="cordova-plugin-globalization"/&gt;
  &lt;plugin name="cordova-plugin-file-transfer"/&gt;
  &lt;plugin name="cordova-plugin-file-opener2"/&gt;
  &lt;preference name="AndroidPersistentFileLocation" value="Compatibility"/&gt;
  &lt;preference name="AndroidExtraFilesystems" value="sdcard,cache,files-external"/&gt;
  &lt;preference name="iosExtraFileSystems" value="bundle,documents"/&gt;
  &lt;access origin="*" /&gt;
  &lt;preference name="Orientation" value="portrait" /&gt;
  &lt;allow-intent href="http://*/*" /&gt;
  &lt;allow-intent href="https://*/*" /&gt;
  &lt;allow-intent href="tel:*" /&gt;
  &lt;allow-intent href="sms:*" /&gt;
  &lt;allow-intent href="mailto:*" /&gt;
  &lt;allow-intent href="geo:*" /&gt;
  &lt;platform name="android"&gt;
      &lt;allow-intent href="market:*" /&gt;
      &lt;preference name="android-minSdkVersion" value="19"/&gt;
      &lt;icon src="www/img/Icon-App.png"/&gt;
  &lt;/platform&gt;
  &lt;platform name="ios"&gt;
      &lt;allow-intent href="itms:*" /&gt;
      &lt;allow-intent href="itms-apps:*" /&gt;
      &lt;preference name="deployment-target" value="8.0"/&gt;
  &lt;/platform&gt;
  &lt;engine name="android" spec="~4.3" /&gt;
  &lt;engine name="ios" spec="~4.1.1" /&gt;
  &lt;plugin name="cordova-sqlite-storage" spec="~1.4.8" /&gt;
  &lt;/widget&gt;
  </code></pre>