R
R
RJs452014-09-03 10:10:40
Zend Framework
RJs45, 2014-09-03 10:10:40

How to display "save as" dialog in ZF2?

There is a form page. The user uploads the file and submits the form. xml file is generated.
How in Zend Framework to save the generated content by the user moss using the "save" dialog and after that the same page was shown to him?
The option with "save to the server, issue a download link" is not suitable.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
HangGlider, 2014-09-03
@RJs45

The ZF2 way involves using the framework code to work with headers, and therefore:

$type = 'text/xml';
$data = '...';
$fileName = 'data.xml';

$response = $this->getResponse();
$response->setContent($data);

$headers = new \Zend\Http\Headers();
$headers->addHeaderLine('Content-Type', $type)
        ->addHeaderLine('Content-Disposition', 'attachment; filename="' . $fileName . '"')
        ->addHeaderLine('Content-Length', strlen($data));

$response->setHeaders($headers);
return $response;

If you are reading from a file, then the first block with $response should be replaced with:
$response = new \Zend\Http\Response\Stream();
$response->setStream(fopen($fileName, 'r'));
$response->setStatusCode(200);

or transfer to work with "X-Accel-Redirect"

S
Sergey Senkevich, 2014-09-03
@ssenkevich

...
$xml = "...";  // XML-данные
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="data.xml"');
header('Cache-Control: max-age=0');

echo $xml;
die();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question