2017-06-02 5 views
0

Je veux ouvrir le fichier pdf sur clic.Comment enregistrer ou ouvrir un fichier pdf en pdf Viewer de la réponse titane

J'ai essayé le code ci-dessous mais cela ne m'a pas aidé. Il donne erreur ne peut pas lire le fichier

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(); 

Mais j'obtiens Erreur comme le chemin du document n'est pas valide.

J'ai essayé de manière différente en utilisant également webview ne recevant toujours pas de pdf sur mon application.

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(); 

Merci d'avance.

Répondre

0

Je triés sur cette

Étape 1: création du répertoire et le fichier.

Étape 2: écrire une réponse au fichier créé.

Étape 3: Ouvrir le pdf avec l'intention android.

est Ci-dessous le code complet

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();