2 回答

TA貢獻1854條經驗 獲得超8個贊
在文檔之前,您可以使用該框架,查看如何將字符串作為文件發送
public function export()
{
$this->autoRender = false;
$id = $this->request->getQuery('id');
$invoice = $this->Invoices->get($id, ['contain' => ['Customers', 'ItemInvoices' => ['ItemProformas' => ['ItemDeliveryNotes' => ['ItemOrders' => ['Orders' => ['Customers']]]]]]]);
$fpr = new ExportInvoice();
$fpr->SetInvoice($invoice);
// header('Content-type: text/xml');
// header('Content-Disposition: attachment; filename="' . $fpr->getFilename() . '"');
$xml = $fpr->asXML();
$response = $this->response;
$response = $response->withStringBody($xml);
// use $response->body($xml); for versions before 3.4.0
$response = $response->withType('xml');
$response = $response->withDownload($fpr->getFilename());
return $response;
}

TA貢獻1911條經驗 獲得超7個贊
只需使用die()或exit()
public function export()
{
$this->autoRender = false;
$id = $this->request->getQuery('id');
$invoice = $this->Invoices->get($id, ['contain' => ['Customers', 'ItemInvoices' => ['ItemProformas' => ['ItemDeliveryNotes' => ['ItemOrders' => ['Orders' => ['Customers']]]]]]]);
$fpr = new ExportInvoice();
$fpr->SetInvoice($invoice);
if (!headers_sent())
{
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="' . $fpr->getFilename() . '"');
}
else
{
//Do something else to let them know they can't expect a file
die();
}
die($fpr->asXML());
}
- 2 回答
- 0 關注
- 148 瀏覽
添加回答
舉報