Angularjs 科尔多瓦(V5.4.0)和#x2B;文件编写器&x2B;FileOpener2+;Base64字符串

Angularjs 科尔多瓦(V5.4.0)和#x2B;文件编写器&x2B;FileOpener2+;Base64字符串,angularjs,cordova,pdf,base64,Angularjs,Cordova,Pdf,Base64,我正在使用AngularJS+Cordova构建一个应用程序 在我的一个控制器中,我执行API调用,该调用返回base64字符串 我需要将Base64字符串解码为PDF格式,并在外部应用程序(例如Adobe PDF Viewer)中显示它 这是我的。我在各个方面都取得了成功,但只要我尝试打开文件,我就一无所获。我甚至找不到文件系统上的文件。好像什么都没有创造出来,但为什么会有那么多成功的信息呢 var pdfDetails = 'a very nice long base 64 string c

我正在使用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");
                    });

最后,我甚至得到了“文件打开成功”。AdobePDF查看器打开,但随后表示找不到该文件。我别无选择。请帮忙

设法让它工作起来

基本上,应用程序将文件写入自己的私有文件夹。我只需要移动它,然后外部应用程序就会打开文件

这是工作代码

$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;
            });
$scope.$on('get-view-attachment-success',函数(事件、数据){
/**实际的base64字符串**/
$scope.attachment_src=data.data.rows.base64;
/**我返回了文档类型,因为它可能是PDF或图像*/
$scope.attachment_type=data.data.rows.type;
如果($scope.attachment_type=='pdf'){
var pdfDetails=$scope.attachment_src;
contentType='application/pdf';
var文件系统;
函数onFs(fs){
文件系统=fs;
/**我�我在这里硬编码文件名**/
getFile('application.pdf',{create:true,exclusive:false},
函数(文件条目){
//为我们的FileEntry(log.txt)创建一个FileWriter对象。
createWriter(函数(fileWriter){
fileWriter.onwriteend=函数(e){
fileEntry.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”;
如果(设备类型!=“iPhone”和设备类型!=“iPad”){
var targetFilePath=cordova.file.externalDataDirectory+file.name;
}否则{
var targetFilePath=cordova.file.tempDirectory+file.name;
}
var ft=新文件传输();
下载(
sourceFilePath,
targetFilePath,
功能(条目){
/**现在我们终于可以打开刚刚创建的文件了**/
cordova.plugins.fileOpener2.open(
targetFilePath,
内容类型,
{
错误:函数(e){
console.log('错误状态:'+e.status+'-错误消息:'+e.message);
},
成功:函数(){
}
}
);
},
函数(错误){
控制台(错误);
}
);
},onError);
};
/**这部分救了我的命。将Base64字符串转换为Blob**/
函数b64toBlob(b64Data,contentType){
contentType=contentType | |“”;
var-sliceSize=512;
b64Data=b64Data.replace(/^[^,]+,/,'');
b64Data=b64Data.replace(/\s/g');
var byteCharacters=window.atob(b64Data);
var ByteArray=[];
对于(变量偏移量=0;偏移量