gpt4 book ai didi

php - 使用 Laravel 生成并下载 XML 文件

转载 作者:行者123 更新时间:2023-12-01 19:24:05 27 4
gpt4 key购买 nike

我正在尝试使用 Laravel 生成 XML 文件。从 Controller 开始,我在 Controller 中生成需要在 XML 文件上使用的数据。我使用 Blade 模板生成 XML 文件,但无法自动保存或下载文件


$xml_datos = [];
$total = 0;

.
.
.

$output = View::make('site.contabilidad.adeudosXML')->with(compact('xml_datos', 'total'))->render();

$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?> \n <Document xmlns=\"urn:iso:std:iso:20022:tech:xsd:pain.008.001.02\">" .$output;

return $xml;

我需要下载文件,而不是显示文件。我尝试过使用存储和文件系统类,但它们似乎都不起作用

最佳答案

您可以创建一个 Response像这样:

 public function saveXML()
{
$output = View::make('site.contabilidad.adeudosXML')->with(compact('xml_datos', 'total'))->render();

$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?> \n <Document xmlns=\"urn:iso:std:iso:20022:tech:xsd:pain.008.001.02\">" .$output;

$response = Response::create($xml, 200);
$response->header('Content-Type', 'text/xml');
$response->header('Cache-Control', 'public');
$response->header('Content-Description', 'File Transfer');
$response->header('Content-Disposition', 'attachment; filename=' . yourFileNameHere . '');
$response->header('Content-Transfer-Encoding', 'binary');
return $response;
}

此外,我强烈建议您使用 PHP functions to manipulate XML .

关于php - 使用 Laravel 生成并下载 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55809347/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com