【发布时间】:2021-07-22 17:37:17
【问题描述】:
我正在尝试根据ajax请求发送的id导出多个word文件。
所以我使用以下脚本:
<script>
$(document).ready(function() {
$('#numero_projet').change(function() {
var id_projet = $(this).find("option:selected").data("id");
$('#generer').click(function() {
alert("hello");
var checkValues = $('input[name=casting_checked]:checked').map(function() {
return $(this).data('id');
})
.get();
var join_selected_values = checkValues.join(",");
$.ajax({
url: "generer/"+id_projet+"/"+checkValues,
type: 'get',
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
data: {
ids: join_selected_values
},
success: function (data) { }
});
});
});
});
</script>
还有以下控制器:
public function generer()
{
if (request()->ajax()) {
$id_projet = request('id_projet');
$id_casting = request('ids');
$idsArray=explode(",",trim($id_casting,','));
if (!empty($idsArray)) {
$id_contrat = Projet_Casting::where('id_projet',$id_projet)->whereIn('id_casting',$i)->value('id_contrat');
$id_modele_contrat = Contrat::where('id_contrat',$id_contrat)->value('id_modele_contrat');
$model_file = Model_Contrat::where('id_modele_contrat',$id_modele_contrat)->value('fichier');
$templateProcessor = new TemplateProcessor('/storage/app/public/uploads/modeles_contrat/'.$model_file);
$filename= $id_contrat;
$templateProcessor->saveAs( $filename.'.docx');
return response()->download( $filename.'.docx');
}
}
}
如果$idsArray 包含 3 个 id,它应该生成 3 个 word 文件。
但是使用这段代码,它只生成一个文档而不是多个文档。
如果你有任何想法,请帮忙。
【问题讨论】:
-
我认为没有办法,除非你使用 zip 或其他东西。您可以尝试多次调用 ajax 请求。
-
如何多次调用ajax请求