So laden Sie Bilder mit Vue.js und Laravel herunter
P粉513318114
P粉513318114 2024-03-19 18:39:21
0
1
400

Ich habe beim Klicken auf diese Schaltfläche eine Hyperlink-Schaltfläche und möchte ein Bild aus der Datenbank abrufen und es mit Laravel und Vue JS auf die Benutzerseite herunterladen. Unten ist mein Skriptdateicode

getImage: function() {
            axios.get('/getImage/' + this.form.cashout_id )
            .then(function (r) 
                {
                const url = window.URL.createObjectURL(new Blob([r.data]));
                const link = document.createElement('a');
                link.href = url;
                link.setAttribute('download', 'file.'+r.headers.ext); //or any other extension
                document.body.appendChild(link);
                link.click();

                //hide loader
                i.loader = false
            })
            .catch(function (error) {
                        alert('Error');
            });
        },

Das ist nun mein Controller-Code, der das Bild erhält.

public function getimage($id)
   { 
       $cashout = CashOutDetail::findorfail($id);
       $storage_date = Carbon::parse($cashout['recorded_date']);

       return response()->download(
           storage_path('app/cashoutdetails/'. $storage_date->year .'/' . $storage_date->format('M') . '/'.  $cashout->bank_receipt),
           'filename.jpg',
           ['Content-Type' => 'image/jpg']
       );
   }

Das Problem ist, dass mein Bild abgerufen und im Konsolenfenster angezeigt, aber nicht heruntergeladen wird. Kann jemand helfen?

P粉513318114
P粉513318114

Antworte allen(1)
P粉391677921

你应该尝试:

axios({
    method: 'GET',
    url: '/getImage/123.jpg',
    responseType: 'blob', //  {
    const url = window.URL.createObjectURL(new Blob([response.data]));
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute('download', '123.jpg');
    document.body.appendChild(link);
    link.click();
  });
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!