使用Laravel生成并下载XML文件

问题描述 投票:1回答:1

我正在尝试使用Laravel生成XML文件。从Controller开始,我在其中生成我需要在XML文件上使用的数据。我使用刀片模板生成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;

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

php xml laravel
1个回答
1
投票

你可以像这样创建一个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

© www.soinside.com 2019 - 2024. All rights reserved.