简体   繁体   中英

How to say browser to start download file?

Using client side I do POST request to Laravel to get back file to browser:

$headers = [
             'Content-Type' => 'application/vnd.ms-excel',
             'Content-Disposition' => "attachment; filename='Report.xls'"
        ];

        return response()->download(storage_path('app/'.$path.$filename), $filename, $headers);

It returns me binary file in response:

在此处输入图片说明

Response headers are:

在此处输入图片说明

Try using:

return Storage::download('file.jpg', $name, $headers);

Reference: https://laravel.com/docs/5.8/filesystem#downloading-files

EDIT 1:

Possible solution:

Create a route for getting a xls document (with the GET http method) by his name for example who returns:

return Storage::download('file.jpg', $name, $headers);

Do a POST request, return the 204 http code and with the Location header.

return response()->header('Location', $url)

When the AJAX success event is called do:

success: function(data, textStatus, request) {
    window.open(request.getResponseHeader('Location'), '_blank');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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