0

I am using drupal as a back end. in drupal I get some .pdf files. and then zip them using drupal zip archive. then saving this archive in my server tmp folder, i get the tmp folder using php sys_get_temp_dir() now... what should i return to the front end (Angular) so that the user can download this folder..

this is an example code i used for zipping:

$nodesIds = [1024, 1023, 1022]; // those are just some example ids, i get the real ids from the front end post request.

$zipName = $this->generateUniqueName(); 
$zip = new \ZipArchive;if(!$zip->open(sys_get_temp_dir() . '/' . $zipName, constant("ZipArchive::CREATE"))) {
    return new JsonResponse('could not open zip');
}

foreach ($nodesIds as $id) {
    $node = \Drupal\node\Entity\Node::load($id);
    $uri = $node->filed_file->entity->getFileUri();
    $name = $node->field_file->entity->id() . '_'. $node->field_file->entity->getFilename();
    $url = $file_system->realpath($uri);
    $zip->addFile($url, $name);
}
$zip->close();

i tried returning a link to the zipped folder in the server:

return new JsonResponse(sys_get_temp_dir() . '/' . $zipName);

but i dont know what to do with that from angular.

Also tried to return a stream using symfony:

$stream  = new Stream(sys_get_temp_dir() . '/' . $zipName);
$response = new BinaryFileResponse($stream);

a stream and not a file because the user chooses which files to zip.. so they can choose as much as they want.. it might get to even a 100 .pdf files ..

every thing works fine and i get the zipped folder in tmp.. but now what ?

I want to download this zipped folder to the user browser.. but I do not know how!. should I return the zipped folder, then in angular use it as a blob or somehow deal with it and serve it to the user,, or maybe the right way is to send back the link to its location in the tmp folder,, and in angular i only access that location and somehow get the folder (i dont know if this is even possible due to permissions and security), or is there another better way that I do not know about.

thank you very much.

khaled
  • 33
  • 1
  • 1
  • 7
  • 2
    Possible duplicate of [send zip file to browser / force direct download](https://stackoverflow.com/questions/7470846/send-zip-file-to-browser-force-direct-download) – ficuscr Apr 26 '19 at 16:48
  • You could also skip writing to temp folder and just serve the output, what you would write to the file... skip the disk I/O... https://stackoverflow.com/questions/3078266/zip-stream-in-php – ficuscr Apr 26 '19 at 16:50

0 Answers0