I am building a cordova application where the user can press a button and pick a file (here named as myfile.txt). Whenever i am pressing the 'Pick a file' button i am getting the error 'Failed to load resource: net::ERR_CONNECTION_REFUSED'. Yes, i did include the cordova file plugin into the project. I dont understand why it needs to connect to the internet. I did try giving permissions explicitly in the config.xml but then the build is getting failed so cannot do that.
My index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; script-src 'self' 'unsafe-inline'; img-src 'self' data: content:;">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="initial-scale=1, width=device-width, viewport-fit=cover">
<meta name="color-scheme" content="light dark">
<style>
#btn{
font-size: 1.5rem;
line-height: 2.5rem;
margin: 1rem;
}
.app{
display:flex;
flex-direction:column;
align-items:center;
text-align:center;
}
</style>
<title>File App</title>
</head>
<body>
<div class="app">
<img src = "img/logo.png" alt=" image" id = "logo"/>
<button id="btn">Pick a File</button>
</div>
<script>
document.addEventListener('deviceready', onDeviceReady, false);
function onDeviceReady() {
document.getElementById('btn').addEventListener('click', pickFile);
}
function pickFile() {
window.resolveLocalFileSystemURL(cordova.file.applicationStorageDirectory, function (dirEntry) {
dirEntry.getFile('myfile.txt', { create: false }, function (fileEntry) {
if (fileEntry) {
fileEntry.file(function (file) {
alert('File picked: ' + file.name);
}, onError);
} else {
createFile();
}
}, onError);
}, onError);
}
function createFile() {
window.resolveLocalFileSystemURL(cordova.file.applicationStorageDirectory, function (dirEntry) {
dirEntry.getFile('myfile.txt', { create: true }, function (fileEntry) {
// File created successfully
}, onError);
}, onError);
}
function onError(error) {
console.error('Error: ' + JSON.stringify(error));
}
</script>
</body>
</html>
My Config.xml
<?xml version='1.0' encoding='utf-8'?>
<widget id="com.example.fileApp" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>FileApp</name>
<description>Sample Apache Cordova App</description>
<author email="[email protected]" href="https://cordova.apache.org">
Apache Cordova Team
</author>
<content src="index.html" />
<access origin="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
</widget>