开发者

Symfony Batch Action

I'm trying to create a batch action (symfony admin) that enables the creation/download on the fly of zip file containing users photos which are avaialable on the uploads/images directory.

Here is the code that I already i开发者_运维技巧mplemented:

public function executeBatchDownloadFotos(sfWebRequest $request)
    {
        $zip = new ZipArchive();

        // atheletes identifiers
        $ids = $request->getParameter('ids');

        // get all the atheletes objects
        $q = Doctrine_Query::create()
            ->from('Atleta a')
            ->whereIn('a.id', $ids);

        foreach ($q->execute() as $atleta)
        {
            $zip->addFile($atleta->id . '_' . $atleta->Clube . '.jpg', 'uploads/atletas/' . $atleta->id . '_' . $atleta->Clube . '.jpg');
        }
    }

By the other hand, here is the view configuration:

BatchDownloadFotos:
  http_metas:
    content-type: application/zip
  has_layout:     false

For some reason, each time execute the batch action, the browser do not prompts me with the window to download the zip file.


After you create ZIP archive in your controller file you should send the content to the browser.

You can do this using methods described here: http://www.symfony-project.org/gentle-introduction/1_4/en/06-Inside-the-Controller-Layer#chapter_06_sub_action_termination

Now you are trying to create ZIP file, but you are not sending it to the browser. You should use setContent() and setHttpHeader() methods.

Your action could look like this (you should add error handling):

public function executeIndex(sfWebRequest $request)
{
  $fileName = '/tmp/test.zip';
  $zip = new ZipArchive();

  $zip->open($fileName, ZipArchive::CREATE);

  // add some files to archive
  $zip->addFile('/tmp/test', 'test.txt');

  $zip->close();

  $this->getResponse()->setContent(file_get_contents($fileName));
  $this->getResponse()->setHttpHeader('Content-Type', 'application/zip');
  $this->getResponse()->setHttpHeader('Content-Disposition',
    'attachment; filename=download.zip');

  return sfView::NONE;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜