簡體   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