如何从响应钛在 pdf 查看器中保存或打开 pdf 文件 [英] How to save or open a pdf file in pdf Viewer from response titanium

查看:37
本文介绍了如何从响应钛在 pdf 查看器中保存或打开 pdf 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想点击打开 pdf 文件.

I want to open pdf file on click.

我尝试了下面的代码,但没有帮助我.它给出错误无法读取文件

I tried below code but it didn't help me. It is giving error could not read file

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

但我收到错误,因为文档路径无效.

But I am getting Error as The document path is not valid.

我尝试了不同的方式也使用 webview 仍然没有在我的应用程序上获取 pdf.

I tried different way also using webview still not getting pdf on my app.

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

提前致谢.

推荐答案

我整理了这个

第一步:创建目录和文件.

Step 1 : Created directory and file.

第 2 步:对创建的文件写入响应.

Step 2 : Write response to created file.

第 3 步:使用 android 意图打开 pdf.

Step 3 : Open pdf with android intent.

以下是完整代码

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

这篇关于如何从响应钛在 pdf 查看器中保存或打开 pdf 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆