breautek commented on issue #426: URL: https://github.com/apache/cordova-plugin-file/issues/426#issuecomment-1386050453
@jfoclpf You're using a jquery API that presumably uses an XHR request to fetch data from a `file://` url. This API is not related to the file plugin at all. The webview doesn't have direct access to `file://` unless if `AndroidInsecureFileModeEnabled` preference is enabled. Enabling this will make Cordova uses the same filesystem based strategy for the webview, which is the same behaviour in cordova-android@9 and older. If `AndroidInsecureFileModeEnabled` is off (the default value), then the webview is handled through a `WebViewAssetLoader`, which is a way of treating your web app as if it was being loaded from a secure `https` site, enabling features that requires a secure context. But this also means that the webview and standard browser features don't have direct access to the file system. While the browser itself doesn't have direct access to the FS, native plugins still does, so if you used the actual cordova plugin API, `cordova.file.applicationDirectory + 'www/json/anomalies.json` will be readable. > mentions replacing file:// by http://localhost. Does this work? If yes, shouldn't cordova.file.applicationDirectory be updated accordingly? `http://localhost` points to the asset directory, which is effectively the equivalent of `file://android_asset/www`. The `http://` version of the path will work in the webview and other browser APIs. But your real issue is that you're mixing APIs together. No, `cordova.file.applicationDirectory` shouldn't be updated because that's part of the File plugin API, which still uses the `file://` protocol, as that is what used on the native side. The `cordova.file.applicationDirectory + 'www/json/anomalies.json'` path will resolve into a proper path that the file plugin can read. Here's an untested example (may have syntax errors or a function name might be wrong, going off memory...) ```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. -- 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]
