我正在使用AngularJS + Cordova构建应用
在我的一个控制器中,我进行API调用,返回一个base64字符串。
我需要获取该Base64字符串,将其解码为PDF,并将其显示在外部应用程序中(例如Adobe PDF Viewer)。
这就是我所拥有的。我全面获得成功,但是一旦我尝试打开文件,我什么也得不到。我甚至无法在文件系统上找到该文件。它似乎没有真正创建,但为什么所有的成功消息呢?
var pdfDetails = 'a very nice long base 64 string comes here';
contentType = 'application/pdf';
var fs;
window.requestFileSystem(LocalFileSystem.PERSISTENT, pdfDetails.length, function (fileSystem) {
fs = fileSystem;
fileSystem.root.getFile("attachment.pdf", { create: true, exclusive: false }, function (fileEntry) {
fileEntry.createWriter(function (writer) {
writer.fileName = "attachment.pdf";
writer.onwrite = function (evt) {
// success!
// BUT WHERE DOES THE FILE SIT? I can't find it anywhere...
cordova.plugins.fileOpener2.open(
fs.root.toURL() + "attachment.pdf",
'application/pdf',
{
error : function(e) {
console.log('Error status: ' + e.status + ' - Error message: ' + e.message);
},
success : function () {
console.log('file opened successfully');
}
}
);
};
writer.onerror = function () {
// handle error
console.log("ERROR 4");
};
writer.write(pdfDetails);
}, function (error) {
// handle error
console.log("ERROR 3");
});
}, function (error) {
// handle error
console.log("ERROR 2");
});
}, function (error) {
// handle error
console.log("ERROR 1");
});
我甚至在这一切结束时都获得了“文件成功打开”。 Adobe PDF Viewer打开,但随后表示无法找到该文件。我没有选择。请帮忙!
答案 0 :(得分:1)
管理以使其正常工作。
基本上,应用程序正在将文件写入其自己的私人文件夹。我只需要移动它,然后外部应用程序就会打开文件。
这是工作代码......
$scope.$on('get-view-attachment-success', function(event, data) {
/** The actual base64 string **/
$scope.attachment_src = data.data.rows.base64;
/** I returned the type of document, cause it could be PDF or Image */
$scope.attachment_type = data.data.rows.type;
if ($scope.attachment_type == 'pdf') {
var pdfDetails = $scope.attachment_src;
contentType = 'application/pdf';
var fileSystem;
function onFs(fs) {
fileSystem = fs;
/** I�m hardcoding the filename here **/
fs.root.getFile('application.pdf', {create: true, exclusive: false},
function(fileEntry) {
// Create a FileWriter object for our FileEntry (log.txt).
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(e) {
fileEntry.file(function(file) {
var sourceFilePath = file.localURL;
//var targetFilePath = fileSystem.root.toURL()+fileDestPath + file.name;
var deviceType = (navigator.userAgent.match(/iPad/i)) == "iPad" ? "iPad" : (navigator.userAgent.match(/iPhone/i)) == "iPhone" ? "iPhone" : (navigator.userAgent.match(/Android/i)) == "Android" ? "Android" : (navigator.userAgent.match(/BlackBerry/i)) == "BlackBerry" ? "BlackBerry" : "null";
if (deviceType != "iPhone" && deviceType != "iPad") {
var targetFilePath = cordova.file.externalDataDirectory + file.name;
} else {
var targetFilePath = cordova.file.tempDirectory + file.name;
}
var ft = new FileTransfer();
ft.download(
sourceFilePath,
targetFilePath,
function(entry){
/** Now we can finally open the file we just created **/
cordova.plugins.fileOpener2.open(
targetFilePath,
contentType,
{
error : function(e) {
console.log('Error status: ' + e.status + ' - Error message: ' + e.message);
},
success : function () {
}
}
);
},
function(error){
console(error);
}
);
}, onError);
};
/** This part saved my life. Covert the Base64 string to a Blob **/
function b64toBlob(b64Data, contentType) {
contentType = contentType || '';
var sliceSize = 512;
b64Data = b64Data.replace(/^[^,]+,/, '');
b64Data = b64Data.replace(/\s/g, '');
var byteCharacters = window.atob(b64Data);
var byteArrays = [];
for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
var slice = byteCharacters.slice(offset, offset + sliceSize);
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
var blob = new Blob(byteArrays, {type: contentType});
return blob;
}
var blob = b64toBlob(pdfDetails , contentType);
fileWriter.write(blob);
}, onError);
}, onError
);
}
if (window.isphone) {
window.requestFileSystem(TEMPORARY, 0, onFs, onError);
}
/** General Error Catcher **/
function onError(err) {
console.log("Oops! : " , err);
}
}
$scope.event_id = data.data.rows.event_id;
});