繁体   English   中英

ExtJS:从Ajax发布请求响应中获取文件

[英]ExtJS: Get file from Ajax post request response

我试图找到一种从extjs应用程序生成pdf文件的方法。 单击我的导出按钮时,将触发此Ajax请求

Ext.Ajax.request({
    url: 'data/apppdf.php?class=Export&action=composerCSV',
    method: 'POST',
    timeout: 1200000,
    params: {
        id: id
    },

    success: function (response, opts) {
        // i don't know what to do here
    }
});

在我的PHP文件中,我使用FPDF库制作pdf文件,如下所示

$pdf = new FPDF();
// fill my file
return $pdf->Output(null, "rapport" . date('Y-m-d_H:i:s'));

在我的请求成功并且脚本返回文件的脚本结束之后,如何使它对用户可用(我如何使用有效的pdf文件获取打开/保存弹出窗口)?

一种方法是将文件保存在服务器上,并在output()函数中使用“ F”作为第一个参数:

$folder = '../';
$dt = date('Y-m-d_H:i:s');
$pdf->Output("F", $folder . "rapport" . $dt);

return $folder . "rapport" . $dt;

由于您现在知道路径和文件名,因此您可以返回此字符串,并在ajax成功函数中根据需要进行处理。 请注意,我首先存储日期时间,因为返回时间可能与创建文件名时的返回时间不同。 请查看文档中的“输出”页面以获取更多信息: http : //www.fpdf.org/en/doc/output.htm

您的Ajax成功功能可以是这样的:

success: function (response, opts) {
    $('div.yourdiv').html(response);
}

这将用php文件的响应替换div.yourdiv中的所有html。 在这种情况下,指向文件的链接。

另一个例子:

success: function (response, opts) {
    location.href = response;
}

创建后,这会将用户重定向到文件。

这是执行此操作的一种方法,我敢保证无需保存文件也是可以的。

尽管可能存在更好的解决方案,但我通常使用这种方法:

  • 定义浏览窗口
  • 在服务器上生成PDF文件
  • 下载和预览PDF文件

ExtJS部分:

Ext.define('Test.ReportWindow', {
    extend: 'Ext.window.Window',

    xfilename: '',
    xtitle: '',

    layout : 'fit',
    width: 1200,
    height: 800,
    maximizable: true,

    initComponent: function() {
        var me = this;

        me.callParent(arguments);
        me.title = me.xtitle;
        me.add(
            {
            xtype: "box",
            autoEl : {tag : "iframe", src : me.xfilename}
            }
        );
    }
});

Ext.onReady(function(){

    Ext.Ajax.request({
        url: 'data/apppdf.php?class=Export&action=composerCSV',
        method: 'POST',
        timeout: 1200000,
        params: {
            id: id
        },

        success: function (response, opts) {
            var r = Ext.JSON.decode(response.responseText);
            if (r.success == true) { 
                var win = Ext.create('Test.ReportWindow', {
                    xfilename: r.filename,
                    xtitle: 'Show PDF file'
                });
                win.show();
            } else {
                Ext.Msg.alert('Attention', r.errmsg);       
            };
        }
    });
}); 

PHP部分:

<?
// Create and save file to disk. Return filename to ExtJS.
// Uncomment next line and generate PDF as you want.
/*
require('fpdf.php');

$filename = 'test.pdf';
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output('F', $filename);

// Exception or other way of error check:
if ($something_is_wrong) {
   echo "{
        success: false,
        errmsg: 'Something is wrong.'
   }";
   exit;
}
*/

echo "{
    success: true, 
    filename: '".addslashes($filename)."'
}";
?>

注意:已使用ExtJS 4.2测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM