jfoclpf opened a new issue, #555:
URL: https://github.com/apache/cordova-plugin-file/issues/555

   based on post from @breautek :
   
   ```javascript
   resolveLocalFilesystemURL(cordova.file.applicationDirectory + 
'www/json/anomalies.json', (fileEntry) => {
       fileEntry.file((file) => {
           let reader = new FileReader();
           reader.onloadend = () => {
               console.log('JSON String', reader.result);
           };
   
           reader.readAsText(file);
       });
   }, (error) => {
       console.error('resolve error', error);
   });
   ```
   
   If you're using the WebViewAssetLoader, the latest version of this plugin as 
a `fileEntry.toURL()` API that also produces a `http(s)://` friendly URLs for 
standard browser APIs to consume.
   
   _Originally posted by @breautek in 
https://github.com/apache/cordova-plugin-file/issues/426#issuecomment-1386050453_
   
   @breautek why reading a file with this plugin needs to be so painful ;) ?
   Can't we have a shortcut, such as in NodeJS, simply 
[readFile](https://www.geeksforgeeks.org/node-js-fs-readfile-method/)
     
   
   I propose the creation of this shortcut to read a file (with Promises)
   
   ```js
   export function readFile (path) {
     return new Promise(function (resolve, reject) {
       window.resolveLocalFilesystemURL(path, (fileEntry) => {
         fileEntry.file((file) => {
           const reader = new FileReader()
           reader.onloadend = () => {
             resolve(reader.result)
           }
           reader.readAsText(file)
   
           reader.addEventListener('error', () => {
             reject(Error(`Error occurred reading file: ${path}`))
           })
         })
       }, (error) => {
         reject(Error('resolve error', error))
       })
     })
   }
   ```
   


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to