I try to export a csv file with some data, but with my current code I get response 200, and some strange characters, and no download, not sure why.
Source URL here.
Exports/DataExport.php
namespace App\Exports;
use App\ViewData;
use Maatwebsite\Excel\Concerns\FromCollection;
use Excel;
class DataExport implements FromCollection
{
/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
return ViewData::all();
}
}
Controller
use App\ViewData;
use App\Exports\DataExport;
use Maatwebsite\Excel\Facades\Excel;
.....
// Export CSV data
public function export()
{
return Excel::download(new DataExport, 'data.xlsx');
}
I added in config/app.php file, service provider and aliase.
You are downloading the csv file from a frontend framework, so ofcourse it will gives you some strange characters instead of downloading a whole file, let me gives you a short code for that
downloadCSV() {
let newWindow = window.open();
axios.get('/json/persons/export')
.then(response => {
newWindow.location = 'http://' + window.location.hostname + '/json/persons/export';
});
}
You need to manually hit that url for the download to work. The rest you understand I think.