the-other-sunny opened a new issue, #374:
URL: https://github.com/apache/cordova-plugin-file-transfer/issues/374

   Hi !
   I'm currently unable to make the plugin work properly.
   `FileTransfer.download` is failing with an error code 3 
(`FileTransferError.CONNECTION_ERR`) and an exception 
`java.lang.NullPointerException`.
   
   ## Minimal example
   
   ```sh
   cordova create . com.example.my_app "My App"
   cordova platform add android
   cordova plugin add cordova-plugin-file-transfer
   ```
   
   Add this child to the `widget` element in `config.xml`.
   
   ```xml
   <access origin="*" />
   ```
   
   Add a call to the `demo` function below from within `onDeviceReady`.
   
   ```js
   function demo() {
       window.requestFileSystem(window.PERSISTENT, 5 * 1024 * 1024,
           fs => fs.root.getFile('cordova_bot.png', { create: true, exclusive: 
false },
               fileEntry => ft_approach(fileEntry),
               error => console.log(`file creation error: ${error}`)
           ),
           (error) => console.log(`fs loading error: ${error}`)
       );
   }
   
   function ft_approach(fileEntry) {
       const fileTransfer = new FileTransfer();
       
       fileTransfer.download(
           'https://cordova.apache.org/static/img/cordova_bot.png',
           fileEntry.toURL(),
           () => window.alert('download complete'),
           error => console.log(`download error: ${JSON.stringify(error, 
undefined, 2)}`),
           false,
       );
   }
   ```
   
   Run the app on the Android Studio `emulator` and attach Chrome DevTools . 
   The output should be something like:
   
   ```
   download error: {
     "code": 3,
     "source": "https://cordova.apache.org/static/img/cordova_bot.png";,
     "target": "https://localhost/__cdvfile_persistent__/cordova_bot.png";,
     "http_status": 200,
     "body": null,
     "exception": "java.lang.NullPointerException"
   }
   ```
   
   ## Workaround
   
   Using web APIs.
   Replacing `ft_approach` with the `xhr_approach` function below works, 
although not idea for large files.
   
   ```js
   function xhr_approach(fileEntry) {
       const xhr = new XMLHttpRequest();
   
       xhr.open('GET', 'https://cordova.apache.org/static/img/cordova_bot.png', 
true);
       xhr.responseType = 'blob';
       xhr.onload = function () {
           const blob = xhr.response;
           if (xhr.status != 200) {
               console.error(`request error ${xhr.status}: ${xhr.statusText}`);
               return;
           }
           fileEntry.createWriter(function (fileWriter) {
               fileWriter.onwriteend = () => window.alert('download complete');
               fileWriter.onerror = error => console.log(`file writing error: 
${error}`);
               fileWriter.write(blob);
           });
       };
       xhr.onerror = function() {
           console.log('request error')
       }
       xhr.send();
   }
   ```
   
   ## Versions
   
   ```
   Windows 10 Home
   Node 18.17.1
   Cordova 12.0.0
   cordova-android 12.0.1
   cordova-plugin-file 8.0.0
   cordova-plugin-file-transfer 2.0.0
   Android SDK 33
   Gradle 8.3
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@cordova.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@cordova.apache.org
For additional commands, e-mail: commits-h...@cordova.apache.org

Reply via email to