0

I'm trying to download files I have this method in my controller

function DownloadLetter(Request $request)
{
    $userId = Auth::id();
    $permission = DB::table("user_permissions")->where('user_id', $userId)->first();

    if ($permission->TL_E) {
        $id = $request->query('id');
        $LettreName = DB::table('jobs')->where('id', $id)->first()->Lettre;
        if ($LettreName) {
            return Storage::download("public/Lettres/" . $LettreName);
        }
    }
}

as you can see I'm returning Storage::download("public/Lettres/" . $LettreName);

and then in my javascript I have

function DownloadLetter(id){
   axios.get("/api/DownloadLetter?id="+id)
      .then(function (response) {
         console.log(response);
       })
}

when I log the response I get the file as JSON, (It doesn't get downloaded)

but when I change the javascript code to this it works

var link = document.createElement("a");
link.download = "";
link.href = '/api/DownloadLetter?id='+id;
document.body.appendChild(link);
link.click();
link.remove();

how can I get the first javascript code to work ?

BenA
  • 21
  • 1
  • 3

0 Answers0