2015-05-18 93 views
1

我試圖在我的應用程序中打開一個PDF文件,我正在用Ionic構建。我明白我可以使用inAppBrowser來做到這一點。如何在Cordova/Ionic應用程序中打開PDF文件?

我首先從api端點獲取PDF,然後將其寫入文件系統。一旦寫入文件系統,我想使用inAppBrowser打開它。我現在的代碼如下:

$http.get(window.apiUrl + '/doc/54bfcc7b879c686969d2bbfe') 
    .success(function(data){ 
     console.log('PDF GOTTEN SUCCESFULLY. NOW WRITE IT TO A FILE.'); 
     $cordovaFile.writeFile('ticket.pdf', data, true) 
      .then(function (success) { 
       console.log('PDF WAS WRITTEN TO THE FILE SYSTEM'); 
       console.log(JSON.stringify(success)); 
       $cordovaFile.readAsText('ticket.pdf') 
        .then(function (data) { 
         console.log('PDF COULD BE READ FROM THE SYSTEM'); 
         window.open('cdvfile://localhost/persistent/ticket.pdf', '_system', 'location=no'); 
         console.log('AFTER OPENING THE PDF'); 
        }, function (error) { 
         console.log(JSON.stringify(error)); 
        }); 
      }, function (error) { 
       console.log(JSON.stringify(error)); 
      }); 
    }) 
    .error(function(error){ 
     console.log('IMAGE GET FAILURE.'); 
    }); 

我在控制檯中沒有任何錯誤,但屏幕上沒有任何反應。我在看的console.log輸出如下:

file:///android_asset/www/js/controllers.js: Line 104 : PDF GOTTEN SUCCESFULLY. NOW WRITE IT TO A FILE. 
file:///android_asset/www/js/controllers.js: Line 110 : PDF WAS WRITTEN TO THE FILE SYSTEM 
file:///android_asset/www/js/controllers.js: Line 111 : {"type":"writeend","bubbles":false,"cancelBubble":false,"cancelable":false,"lengthComputable":false,"loaded":0,"total":0,"target":{"fileName":"","length":598874,"localURL":"cdvfile://localhost/persistent/ticket.pdf","position":598874,"readyState":2,"result":null,"error":null,"onwritestart":null,"onprogress":null,"onwrite":null,"onabort":null,"onerror":null}} 
file:///android_asset/www/js/controllers.js: Line 115 : PDF COULD BE READ FROM THE SYSTEM 
file:///android_asset/www/js/controllers.js: Line 119 : AFTER OPENING THE PDF 

正如你可以看到我硬編碼了傳票名稱(ticket.pdf),並在打開時它的路徑(cdvfile://localhost/persistent/ticket.pdf)。我從json success-output得到的文件路徑顯示在111行的輸出中(見上)。

有人知道爲什麼這不起作用嗎?歡迎所有提示!

回答

0

url cdvfile://localhost/persistent/ticket.pdf不正確。您需要一個以file://開頭的nativeurl路徑。

嘗試這些:

'文件:///storage/emulated/0/ticket.pdf' 或 '文件:///storage/sdcard0/ticket.pdf'

相關問題