I am currently developing a mobile application using Cordova. In the app, there is a tutorial available in .docx format that needs to be accessed on the device. The current method involves the user clicking a button to view the tutorial document, which then opens in any compatible application for .docx files installed on the device. Strangely, this functionality works smoothly on Android devices but encounters an issue on iOS. An error code 3 is thrown during the download process, specifically within the FileTransferError, but this occurs only on iOS and not on Android. I have conducted research on this matter, but most resources focus on downloading from web servers. Can anyone explain why I am encountering error code 3 on iOS while experiencing no issues on Android?
Below is the code snippet triggered when the tutorial viewing button is clicked:
function downloadTutorial() {
var userAgent = navigator.userAgent || navigator.vendor || window.opera;
if (userAgent.indexOf("Android") > -1) {
window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function (directoryEntry) {
directoryEntry.getFile("app-tutorial.docx", { create: true }, function (fileEntry) {
download(fileEntry, encodeURI(cordova.file.applicationDirectory + "www/docs/app-tutorial-en.docx"));
}, function(e1){
console.error(`get file failed`);
console.log(e1);
});
}, function(e) {
console.error(`write failed`);
console.log(e);
});
}
else if ((userAgent.indexOf("iPad") > -1 || userAgent.indexOf("iPhone") > -1 || userAgent.indexOf("iPod") > -1) && !window.MSStream) {
window.resolveLocalFileSystemURL(cordova.file.documentsDirectory, function (directoryEntry) {
console.log(directoryEntry);
directoryEntry.getFile("app-tutorial.docx", { create: true }, function (fileEntry) {
console.log(fileEntry);
download(fileEntry, encodeURI(cordova.file.applicationDirectory + "www/docs/app-tutorial-en.docx"));
}, function(e1){
console.error(`get file failed`);
console.log(e1);
});
}, function(e) {
console.error(`write failed`);
console.log(e);
});
}
}
This section details the download function used:
function download(fileEntry, uri) {
var fileTransfer = new FileTransfer();
var fileURL = fileEntry.toURL();
console.log(fileURL);
var options = new FileUploadOptions();
options.headers = { Connection: "close" };
fileTransfer.download(uri, encodeURI(fileURL), function (entry) {
console.log("Successful downloaded tutorial file.");
cordova.plugins.fileOpener2.open(
fileURL,
"application/vnd.openxmlformats-officedocument.wordprocessingml.document", {
error : function(e) {
console.error(`Error status: ${e.status} - Error message: ${e.message}`);
},
success : function () {
console.log('file opened successfully');
}
}
);
},
function (error) {
console.error(`file transfer error ${error}`); // a FileTransferError is logged here with the source, destination and error code 3
}, false, options);
}
The following excerpt is extracted from config.xml:
<widget id="" version="0.18.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name></name>
<description>
</description>
<author email="" href="">
</author>
<content src="index.html" />
<plugin name="cordova-plugin-whitelist" spec="1" />
<plugin name="cordova-plugin-console" spec="1.0.3" />
...
</widget>