dtja73027 2019-04-26 16:42
浏览 71

是否可以从服务器(php)发送一个压缩文件夹到前端(角度)? 还是有另一种方法可以做到这一点

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
ode\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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
    • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
    • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
    • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
    • ¥15 运动想象脑电信号数据集.vhdr
    • ¥15 三因素重复测量数据R语句编写,不存在交互作用
    • ¥15 微信会员卡等级和折扣规则
    • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
    • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
    • ¥15 gdf格式的脑电数据如何处理matlab