Я использую следующий код:
Код: Выделить всё
$(document).on('click', '.download', function(){
var data = '';
var id = $(this).attr('id');
console.log(id);
var data = '';
$.ajax({
url:"modeles_contrat/"+id+"/download",
type:"GET",
data: data,
xhrFields: {
responseType: 'blob'
},
success: function(response) {
var blob = new Blob([response]);
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = "casting.docx";
link.click();
},
error: function() {
$('#responseMsg').removeClass("alert-success");
$('#responseMsg').addClass("alert-danger");
$('#responseMsg').html('Veuillez générer le contrat');
$('#responseMsg').show();
}
})
});
Код: Выделить всё
public function download($id, $downloadName = null) {
$data2 = array();
$data_modele = Model_Contrat::where('id_modele_contrat',$id)->first();
$filename= $data_modele->fichier;
$filepath = 'Casting_V0.1_Test\casting\storage\app\public\uploads\modeles_contrat/'.$filename;
/* $filepath = str_replace('', '/',public_path('Model/'.$filename.'.docx'));
*/
if (file_exists($filepath)) {
$headers = array(
'Content-Type: application/docx',
);
$downloadName = $downloadName??$filename;
return Response::download($filepath);
} else {
$data2['success'] = 2;
$data2['message'] = 'File not uploaded.';
return response()->json($data2);
}
}
Если у вас есть идеи о том, как я могу загружать файлы с собственным именем, помогите мне. .
Заранее спасибо
ИЗМЕНИТЬ
Я пытался удалить вызов ajax. Поэтому я использую следующий код:
В свою таблицу данных я добавил ссылку на столбец, чтобы получить идентификатор строки:
Код: Выделить всё
$button .='[url=/modeles_contrat/]id_modele_contrat.'"/download" class="btn btn-xs btn-info pull-right" data_id="'.$data->id_modele_contrat.'" >Télécharger[/url]';
Код: Выделить всё
Route::group(['middleware' => ['auth','role:account_manager|admin|manager_de_filiale']], function() {
Route::get('/modeles_contrat/{id_modele_contrat}/download', [App\Http\Controllers\ModeleController::class, 'download']);
});
Код: Выделить всё
public function download($id_modele_contrat) {
$data2 = array();
$data_modele = Model_Contrat::where('id_modele_contrat',$id_modele_contrat)->first();
$filename = $data_modele->fichier;
$filepath = 'Casting_V0.1_Test\casting\storage\app\public\uploads\modeles_contrat/'.$filename;
/* $filepath = str_replace('', '/',public_path('Model/'.$filename.'.docx'));
*/
if (file_exists($filepath)) {
$headers = array(
'Content-Type: => application/docx',
);
return Response::download($filepath,$filename, $headers);
}
}

Подробнее здесь: https://stackoverflow.com/questions/693 ... rd-library