【问题标题】:How to save or open a pdf file in pdf Viewer from response titanium如何在响应钛的 pdf 查看器中保存或打开 pdf 文件
【发布时间】:2017-06-02 11:39:25
【问题描述】:

我想点击打开 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();

提前致谢。

【问题讨论】:

    标签: titanium titanium-alloy


    【解决方案1】:

    我整理好了

    第 1 步:创建目录和文件。

    第 2 步:将响应写入创建的文件。

    第 3 步:使用 android 意图打开 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();
    

    【讨论】:

      猜你喜欢
      • 2022-06-11
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 2020-05-05
      • 1970-01-01
      • 1970-01-01
      • 2018-02-28
      • 1970-01-01
      相关资源
      最近更新 更多