我实际上需要在混合应用程序中以pdf格式下载文档。我在其中添加了自定义javascript下载功能。我添加的脚本实际上在这里遇到了问题。我已经附上了脚本供您参考,下面总是会出现错误提示文字。
FileEntryfilesystem: FileSystemfullPath: "/Download/NaturalResource.pdf"isDirectory: falseisFile: truename: "NaturalResource.pdf"nativeURL: "file:///storage/emulated/0/Download/NaturalResource.pdf"__proto__: Entryconstructor: (name, fullPath, fileSystem, nativeURL)createWriter: (successCallback, errorCallback)file: (successCallback, errorCallback)__proto__: Object
cordova_android.js:312 Error in Success callbackId: File581003392 : TypeError: downloadFile is not a function
cordova_android.js:314 Uncaught TypeError: downloadFile is not a function
at custom.js:97
at win (DirectoryEntry.js:110)
at Object.callbackFromNative (cordova_android.js:293)
at processMessage (cordova_android.js:1081)
at processMessages (cordova_android.js:1104)
at pollOnce (cordova_android.js:973)
at pollOnceFromOnlineEvent (cordova_android.js:960)
at downloadStatementInMobileApp (custom.js:102)
at Channel.onDeviceReady (custom.js:5)
at Channel.fire (cordova_android.js:822)
(anonymous) @ custom.js:97
win @ DirectoryEntry.js:110
callbackFromNative @ cordova_android.js:293
processMessage @ cordova_android.js:1081
processMessages @ cordova_android.js:1104
pollOnce @ cordova_android.js:973
pollOnceFromOnlineEvent @ cordova_android.js:960
downloadStatementInMobileApp @ custom.js:102
onDeviceReady @ custom.js:5
Channel.fire @ cordova_android.js:822
(anonymous) @ cordova_android.js:231
代码从这里开始
function downloadStatementInMobileApp() {
var loginViaMobile = localStorage.getItem("MobileApp");
if (loginViaMobile == 'true') {
var userAgent = navigator.userAgent || navigator.vendor || window.opera;
var documentDir;
var fileDir;
var fileURI = "https://www.kcesmjcollege.in/ICT/Geography/Natural%20Rsources.pdf";
if (/android/i.test(userAgent)) {
documentDir = cordova.file.externalRootDirectory;
fileDir = 'Download';
} else if (/iPhone/.test(userAgent) && !window.MSStream) {
documentDir = cordova.file.documentsDirectory;
fileDir = 'Documents';
}
window.resolveLocalFileSystemURL(documentDir, function(fs) {
fs.getDirectory(fileDir, {
create: true,
exclusive: false
}, function(directory) {
directory.getFile("NaturalResource.pdf", {
create: true,
exclusive: false
}, function(fileEntry) {
console.log(fileEntry);
downloadFile(fileEntry, fileURI)
}, onFileTranferError);
}, onFileTranferError);
}, onFileTranferError);
var downloadFile = function(fileEntry, uri) {
var fileTransfer = new FileTransfer();
var fileURL = fileEntry.toURL();
fileTransfer.download(
uri,
fileURL,
function(entry) {
logDebugMessage('file transfer complete: ' + entry.toURL());
// navigator.notification.alert('Your file has been stored in the '+fileDir+' folder', null, 'Download Success', 'Done');
openPDFViewer(entry);
},
onFileTranferError,
null // or, pass false
);
};
var openPDFViewer = function(fileEntry) {
var url = fileEntry.toURL();
var onError = function(error) {
console.log(error);
};
var mimeType = 'application/pdf';
cordova.plugins.fileOpener2.open(url, mimeType, {
error: function(e) {
console.log(e);
},
success: function() {}
});
};
var onFileTranferError = function(error) {
navigator.notification.alert('Sorry something went wrong', null, 'Download Failed', 'Done');
console.log('download error source ' + error.source);
console.log('download error target ' + error.target);
console.log('download error code' + error.code);
};
var logDebugMessage = function(message) {
var debug_mode = false;
if (debug_mode) {
console.log(message);
}
};
}
}
我没有得到successCallBack,这就是为什么我失败了。但我不确定。请分享您对此的见解。
插件文件传输会返回此sucessCallBack吗?