[Vuejs]-Laravel Downloaded file from the browser gets corrupted

0👍

I had to use standard XMLHttpRequest to work it like below.

downloadFile() {

            var app = this;
            var url = baseRoute + 'files/' + this.fileId + '/download';
            var xhr = new XMLHttpRequest();
            xhr.open("POST", url);
            xhr.setRequestHeader('Authorization', 'bearer ' + this.Authentication.tokenData().token);
            xhr.responseType = "arraybuffer";

            xhr.onload = function () {

                if (this.status === 200) {

                    var today = ((new Date()).toISOString()).slice(2,10),
                        filename = today.replace(/-/gi, "")+' '+ app.fileTitle;

                    var blob = new Blob([xhr.response], {type: xhr.getResponseHeader('content-type')});
                    var objectUrl = URL.createObjectURL(blob);

                    let link = document.createElement('a');
                    link.href = objectUrl;
                    link.download = filename;
                    link.click();
                    window.URL.revokeObjectURL(blob);
                }
            };
            xhr.send();
        },

Leave a comment