简体   繁体   English

Laravel从浏览器下载的文件已损坏

[英]Laravel Downloaded file from the browser gets corrupted

I am trying to download the file saved in s3. 我正在尝试下载保存在s3中的文件。 So I have codes like below. 所以我有下面的代码。

static public function download($key, $disk = 's3') : object
{

    $file = FileHelper::get($key);

    $local = Storage::disk('local');
    $local->put(basename($key), $file);
    $pathToFile = storage_path().'/app/'.basename($key);

    $headers = [
        'Content-Type' => \GuzzleHttp\Psr7\mimetype_from_filename(basename($key)),
        'Content-Description' => 'File Transfer',
        'Content-Disposition' => "attachment; filename=' ". basename($key),
        'filename'=> basename($key)
    ];

    $repsonse = response()->download($pathToFile, basename($key), $headers)->deleteFileAfterSend(true);
    ob_end_clean();

    return $repsonse;
}

And I have the javascript code like below. 而且我有如下的javascript代码。

downloadFile() {
            var app = this;

            var url = baseRoute + 'files/' + this.fileId + '/download';
            axios.get(url, {
                headers: {
                    Authorization: 'bearer ' + this.Authentication.tokenData().token,
                    responseType : "blob"
                }
            }).then(function (response) {

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

                let blob = new Blob([response.data], {
                    type: response.headers['content-type']
                });

                let link = document.createElement('a');
                link.href = window.URL.createObjectURL(blob);
                link.download = filename;
                link.click();

            }).catch(function (response) {
                console.log(response);
            });
        }

The file is downloaded, however, the file is corrupted. 已下载文件,但是文件已损坏。 (it would not open) I checked S3 bucket to see if the file is corrupted during upload, but it was fine there. (它不会打开)我检查了S3存储桶,以查看文件在上传过程中是否已损坏,但是在那很好。

What am I doing wrong? 我究竟做错了什么? Any advice or suggestion would be appreciated. 任何意见或建议,将不胜感激。

Thank you in advance. 先感谢您。

Added: when I tried to view the image, it returned white squared which means its base64 encoded image. 补充:当我尝试查看图像时,它返回白色方形,这表示它的base64编码图像。 Does it have to do with being empty file? 与空文件有关吗?

I had to use standard XMLHttpRequest to work it like below. 我不得不使用标准的XMLHttpRequest来像下面那样工作。

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();
        },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM