【问题标题】:Customise the filename in phpword自定义phpword中的文件名
【发布时间】:2020-11-01 04:47:43
【问题描述】:

我想下载的时候可以自定义phpword中的文件名吗?

我希望文件采用导出行的prenoms

我的代码:

public function edit (Stagiaire $stagiaire)
  {
    $id = $stagiaire ->id;
    $desc1 = Stagiaire::find($id);
    $my_template = new \PhpOffice\PhpWord\TemplateProcessor(public_path('templateStagiaire.docx'));
    $my_template->setValue('id, $desc->id);
    $my_template->setValue('prenoms, $desc->prenoms);
    $my_template->setValue('nom, $desc->nom);

    $filename = $stagiaire->prenoms;
    try{
       $my_template->saveAs(storage_path('templateStagiaire.docx'));
       }catch (Réception $e){}
     return response()->download(storage_path('".$filename.".docx));

     }

需要帮助。 提前致谢。

【问题讨论】:

    标签: laravel phpword


    【解决方案1】:

    Laravel 下载文件示例:

    
        public function edit(Stagiaire $stagiaire, $downloadName = null)
        {
            $id = $stagiaire->id;
            $desc = Stagiaire::find($id);
            $my_template = new \PhpOffice\PhpWord\TemplateProcessor(public_path('templateStagiaire.docx'));
            $my_template->setValue('id', $desc->id);
            $my_template->setValue('prenoms', $desc->prenoms);
            $my_template->setValue('nom', $desc->nom);
    
            // save as `prenoms` filename
            $filename = $stagiaire->prenoms;
            try {
                $my_template->saveAs(storage_path("$filename.docx"));
            } catch (Reception $e) {
            }
    
            // if download name is null, then use filename
            $downloadName = $downloadName??$filename;
            
            return response()->download(storage_path("$filename.docx"))
                ->header('Content-disposition','attachment; filename="'.$downloadName.'"');
    
         }
    
    

    Similar Laravel example

    【讨论】:

    • 非常感谢。它工作得很好。但是我修改了一些东西。我删除了这个:->header('Content-disposition','attachment; filename="'.$downloadName.'"');
    • 是的,这正是答案
    猜你喜欢
    • 1970-01-01
    • 2021-11-20
    • 2019-01-02
    • 2019-06-30
    • 1970-01-01
    • 2015-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多