Laravel-excel: Lumen 5.6 exporting as file instead of binary response

Created on 23 Feb 2019  Â·  11Comments  Â·  Source: Maatwebsite/Laravel-Excel

Hi contributor I have read and used your library that is really a very good piece of code to utilize in project. I have done import part but I am stuck on exporting part can someone help me that which thing I am doing wrong;
what I am using

  • Lumen 5.6
  • Laravel-Excel 3.1.7

In my Export Class

<?php
namespace App\Modules\Marketing\Requests\Submission;

use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithMapping;
use App\Modules\Marketing\Models\SubmissionModel;

class SubmissionExport implements FromQuery, WithMapping
{

    use Exportable;

    public function __construct($requestData, $dataMap = [])
    {
        $this->requestData = $requestData;
        $this->dataMap = $dataMap;
    }

    public function map($data): array
    {
        $mappedData = [];
        foreach ($this->dataMap as $key => $value) {
            $tempArray[] = $data->$value;
        }
        return $tempArray;
    }

    public function query()
    {
        $response = SubmissionModel::query();
        //here check if directory param is given
        foreach ($this->requestData as $key => $field) {
                $response = $response->where($key, $field);
        }
        return $response;
    }
}

In my Request Class

namespace App\Modules\Marketing\Requests\Submission;

use App\Core\Base\Request;
use App\Modules\Marketing\Models\SubmissionModel;
use Excel;

class ExportRequest extends Request
{

    private $submissionModel;

    function __construct()
    {
        $this->submissionModel = new SubmissionModel();
    }

    public function process($requestData)
    {
        //here transform the response in data
        return Excel::download(new \App\Modules\Marketing\Requests\Submission\SubmissionExport($requestData, ['title','description']), 'submisssion.xlsx');
    }

Here in request class I am using Excel because I have defined class_alias(Maatwebsite\Excel\Facades\Excel::class, 'Excel'); in bootstrap/app.php
Specified request return me data in binary form instead of dowlaoding file as submission.xlsx as

o�WNG�D�Xo�WNG�D�Xo�WNG�D�Xo�WNG�D�Xo�WNG�D�Xo�WNG�D�Xo�WNG�D�Xo�WNG�D�Xo�WNG�D�Xo�WNG�D�Xo�WNG�D�Xo�WNG�D�Xo�WNG�D�Xo�WNG�D�X

and so on more data like this. Please help me how I can download this file.

question

All 11 comments

Thanks for submitting the ticket. Unfortunately the information you provided is incomplete. We need to know which version you use and how to reproduce it. Please include code examples. Before we can pick it up, please check (https://github.com/Maatwebsite/Laravel-Excel/blob/3.1/.github/ISSUE_TEMPLATE.md) and add the missing information. To make processing of this ticket a lot easier, please make sure to check (https://laravel-excel.maatwebsite.nl/3.1/getting-started/contributing.html) and double-check if you have filled in the issue template correctly. This will allow us to pick up your ticket more efficiently. Issues that follow the guidelines correctly will get priority over other issues.

Hey @mudasserzahid we currently don't really support Lumen, however we have in the past added some things to improve support for Lumen. I haven't tested downloads in Lumen, but I'm assuming it should work the same as in Laravel. download() returns a BinaryFileResponse in Laravel. (Whichs triggers a download) You could try and dd(Excel::download() and see if that's the same for Lumen.

Make sure the actual Excel::download response is returned in the controller and not converted to a string or something. I'm not familiar with the Request::process method you are using, if that's being called in a route or controller, make sure you return that response.

Thanks for your response @patrickbrouwers. I think we should test this library also with API point of view.
Here are some solution which I have tried with this library while returning the binary file. I set the response header as Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet . That thing was helpful in case receiving file in attachments like
http://prntscr.com/mpbq6q
but issue is still same there is no file download in request.
After solution given by you and Here is the response after dd(Excel::(download)
http://prntscr.com/mpbqgd
Let me know more information, that we can make this library more compatible. Happy coding :+1:

How are you requesting the download, are you doing an ajax call from the frontend?

I am doing specified request using Postman.

FYI. Here is some testing which has done by my side, I changed something in Maatwebsite\Excel\Writer, there is a method named tempFile. I just concatenated .xls with string.
Now it is storing the accurate output in temp directory like if I do,

$response = Excel::download();
print_r($response->getFile());

Specified code will return me a string (path to excel file in temp directory). Further I am still reading your library.

If you use postman/xhr/ajax call than it's logical you see the binary response. If you want to download the excel file in the frontend, you should let people directly call that URL,not via an ajax call.

Yes, you are right in your point, but I need this as an API point of view. Because Lumen is for writing web services. In other cases, I just explored some libraries like mPDF what they are doing in Lumen while the download is as,

$output = $this->output();
return new Response($output, 200, array(
        'Content-Type' => 'application/pdf',
        'Content-Disposition' =>  'attachment; filename="'.$filename.'"'
));

in our case this should work like this

$response = Excel::download(new ExportTransfer, 'submissions.xls');
$response->sendHeaders(array(
      'Content-Type: application/vnd.ms-excel',
      'Content-Disposition: attachment; filename="submissions.xls"'
));
return $response;

But still there is not any proper response, in header we can see a file but physically there is no existance of file.

You could try to use Excel::raw and stream that ?

I have not tried Excel::raw there is not any method named as in the library.

Here is the solution of specified exception, the thing about which I am saying was a bad response actually is an accurate response. All the thing you need is work with Blob on front and export file to your desired extension. For the time being, I am using Angular JS as a front-end application. I will share also source code for Angular JS if that would be relevant to this repository.

Was this page helpful?
0 / 5 - 0 ratings