Heim  >  Fragen und Antworten  >  Hauptteil

So laden Sie Bilder mit Vue.js und Laravel herunter

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粉513318114P粉513318114213 Tage vor407

Antworte allen(1)Ich werde antworten

  • P粉391677921

    P粉3916779212024-03-20 09:28:48

    你应该尝试:

    axios({
        method: 'GET',
        url: '/getImage/123.jpg',
        responseType: 'blob', // <-<<<<<<<<<< 
      }).then((response) => {
        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();
      });

    Antwort
    0
  • StornierenAntwort