2017-06-02 23 views
0

クリックしてpdfファイルを開きます。レスポンスチタンからpdfビューアにpdfファイルを保存する方法

私は以下のコードを試しましたが、それは私を助けませんでした。エラーがファイルを読み取ることができませんでした。

var xhr = Ti.Network.createHTTPClient({ 
    onload : function(e) { 
     var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'file.pdf'); 
     Ti.API.info('file == ' + f); 
     Ti.API.info('response = ' + this.responseData); 
     Ti.API.info('response = ' + JSON.stringify(this.responseData)); 
     f.write(this.responseData); 
     Ti.API.info('write file == ' + f.write(this.responseData)); 
     Ti.API.info('filepath == ' + f.nativePath); 
     Ti.API.info('get filepath == ' + f.getNativePath()); 
     Ti.Android.currentActivity.startActivity(Ti.Android.createIntent({ 
      action : Ti.Android.ACTION_VIEW, 
      type : 'application/pdf', 
      data : f.nativePath 
     })); 
    }, 
    onerror : function(e) { 
     Alloy.Globals.loading.hide(); 
     alert("Cannot retrieve PDF form web site"); 
    }, 
    timeout : 5000 
}); 

xhr.open('GET', "https://www.mbta.com/uploadedfiles/Documents/Schedules_and_Maps/Rapid%20Transit%20w%20Key%20Bus.pdf"); 
xhr.send(); 

ただし、ドキュメントパスが無効です。

私のアプリでwebviewを使っていても、まだpdfが得られませんでした。

var win = Ti.UI.createWindow({ 
    backgroundColor : '#fff' 
}); 

var pdfViewer = Ti.UI.createWebView({ 
    url : "http://lkn.ccomsys.com/assets/media/datafiles/gov/vvvv_Shehnai_Order1.pdf", 
    width : Titanium.UI.SIZE, 
    height : Titanium.UI.SIZE 
}); 
Ti.API.info('pdfviewer == ' + JSON.stringify(pdfViewer)); 
win.add(pdfViewer); 

win.open(); 

ありがとうございます。

答えて

0

これをソートしました

手順1:作成されたディレクトリとファイル。

手順2:作成したファイルへの応答を書き込みます。

手順3:アンドロイドインテントでpdfを開きます。以下は

は、完全なコード

var xhr = Ti.Network.createHTTPClient({ 
    onload : function(e) { 
     Alloy.Globals.loading.hide(); 
     var Settings = Titanium.Filesystem.getFile(Titanium.Filesystem.tempDirectory, 'Settings'); 
     Ti.API.info("Created Settings: " + Settings.createDirectory()); 
     Ti.API.info('Settings ' + Settings); 
     var newFile = Titanium.Filesystem.getFile(Settings.nativePath, 'Setting.pdf'); 
     Ti.API.info('new file == ' + newFile); 
     if (newFile.exists() === false) { 
      // you don't need to do this, but you could... 
      newFile.write(this.responseData); 
     } 

     Ti.API.info('response = ' + this.responseData); 
     Ti.API.info('response = ' + JSON.stringify(this.responseData)); 

     if (newFile.exists()) { 
      newFile.write(this.responseData); 
      Ti.API.info('newfile: ' + newFile.read()); 
     } 
     Ti.Android.currentActivity.startActivity(Ti.Android.createIntent({ 
      action : Ti.Android.ACTION_VIEW, 
      type : 'application/pdf', 
      data : newFile.nativePath 
     })); 
    }, 
    onerror : function(e) { 
     Alloy.Globals.loading.hide(); 
     alert("Cannot retrieve PDF form web site"); 
    }, 
    timeout : 5000 
}); 

xhr.open('GET', "https://www.mbta.com/uploadedfiles/Documents/Schedules_and_Maps/Rapid%20Transit%20w%20Key%20Bus.pdf"); 
xhr.send(); 
です
関連する問題