ExtJS:从 Ajax post 请求响应中获取文件
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 文件获得打开/保存弹出窗口)?
一种方法是将文件保存在您的服务器上,将 "F" 作为 output() 函数中的第一个参数:
$folder = '../';
$dt = date('Y-m-d_H:i:s');
$pdf->Output("F", $folder . "rapport" . $dt);
return $folder . "rapport" . $dt;
由于您现在知道路径和文件名,您可以 return 这个字符串并在 ajax 成功函数中随意使用它。
请注意,我首先存储日期时间,因为 return 时间可能与创建文件名时不同。
查看文档中的输出页面以获取更多信息:http://www.fpdf.org/en/doc/output.htm
您的 Ajax 成功函数可以是这样的:
success: function (response, opts) {
$('div.yourdiv').html(response);
}
这会将 div.yourdiv 中的所有 html 替换为来自 php 文件的响应。在这种情况下,link 到您的文件。
另一个例子:
success: function (response, opts) {
location.href = response;
}
这将在创建后将用户重定向到文件。
这是一种方法,我相信不保存文件也是可行的。
我通常使用这种方法,尽管可能存在更好的解决方案:
- 定义浏览window
- 在服务器上生成 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 测试。
我尝试找到一种从我的 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 文件获得打开/保存弹出窗口)?
一种方法是将文件保存在您的服务器上,将 "F" 作为 output() 函数中的第一个参数:
$folder = '../';
$dt = date('Y-m-d_H:i:s');
$pdf->Output("F", $folder . "rapport" . $dt);
return $folder . "rapport" . $dt;
由于您现在知道路径和文件名,您可以 return 这个字符串并在 ajax 成功函数中随意使用它。 请注意,我首先存储日期时间,因为 return 时间可能与创建文件名时不同。 查看文档中的输出页面以获取更多信息:http://www.fpdf.org/en/doc/output.htm
您的 Ajax 成功函数可以是这样的:
success: function (response, opts) {
$('div.yourdiv').html(response);
}
这会将 div.yourdiv 中的所有 html 替换为来自 php 文件的响应。在这种情况下,link 到您的文件。
另一个例子:
success: function (response, opts) {
location.href = response;
}
这将在创建后将用户重定向到文件。
这是一种方法,我相信不保存文件也是可行的。
我通常使用这种方法,尽管可能存在更好的解决方案:
- 定义浏览window
- 在服务器上生成 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 测试。