document.addEventListener('deviceready', onDeviceReady, false); function onDeviceReady() { const fileTransfer = new FileTransfer(); const uri = encodeURI("https://example.com"); // Define where the file should be saved // dataDirectory is generally private to the app const fileURL = cordova.file.dataDirectory + "document.pdf"; fileTransfer.download( uri, fileURL, function(entry) { console.log("Download complete: " + entry.toURL()); alert("File downloaded to: " + entry.fullPath); }, function(error) { console.log("Download error source: " + error.source); console.log("Download error target: " + error.target); console.log("Download error code: " + error.code); }, false, { // Optional: add headers if needed // headers: { "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA==" } } ); } Use code with caution. Implementing a Progress Bar
One of the main reasons developers still use this plugin is the built-in onprogress listener, which makes it easy to update a UI progress bar. javascript cordova-plugin-file-transfer download example
async function downloadFile(url, fileName) { const response = await fetch(url); const blob = await response.blob(); window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function(dirEntry) { dirEntry.getFile(fileName, { create: true, exclusive: false }, function(fileEntry) { fileEntry.createWriter(function(fileWriter) { fileWriter.write(blob); console.log("File saved via Fetch!"); }); }); }); } Use code with caution. document
: (Optional, Boolean) Set to true to accept all security certificates (useful for self-signed certs in dev environments). : (Optional, Boolean) Set to true to accept