Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel download a newly created xmlfile

I am trying to create xmlfile and download it from my program but it isnt working very well.

This is my code:

$xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><mydoc></mydoc>');

    $xml->addAttribute('version', '1.0');
    $xml->addChild('datetime', date('Y-m-d H:i:s'));

    $person = $xml->addChild('person');
    $person->addChild('firstname', 'Someone');
    $person->addChild('secondname', 'Something');
    $person->addChild('telephone', '123456789');
    $person->addChild('email', '[email protected]');

    $address = $person->addchild('address');
    $address->addchild('homeaddress', 'Andersgatan 2, 432 10 Göteborg');
    $address->addChild('workaddress', 'Andersgatan 3, 432 10 Göteborg');

    $xml->saveXML('test.xml');

    $response = Response::make($xml->asXML(), 200);
    $response->header('Content-Type', 'text/xml');

    return $response;

This is showing the xmlfile in my browser, but i want a popup to appear, asking me to download the file i just created. I tried it with Response::download() to but that isnt working neither because the first parameter should be the path to the file then.

Any help would be appreciated.

like image 455
Vincent Avatar asked Oct 09 '13 13:10

Vincent


1 Answers

I think you might need to add some more headers:

$response->header('Cache-Control', 'public');
$response->header('Content-Description', 'File Transfer');
$response->header('Content-Disposition', 'attachment; filename=test.xml');
$response->header('Content-Transfer-Encoding', 'binary');
$response->header('Content-Type', 'text/xml');
like image 183
Antonio Carlos Ribeiro Avatar answered Nov 04 '22 00:11

Antonio Carlos Ribeiro