Reputation: 321
I am Doting Excel export using Laravel with Vuejs, somehow the Code is returning true value but can not Download Excel file, if I do normal request it will download the file, but in axios request, it will not export the file
I am using php artisan make:export to export file
In App/Export/studentexport.php
public function collection()
{
return Student_master::all();
}
then in controller i will do a function
public function export()
{
return Excel::download(new StudentExport, 'users.xlsx');
}
In my Vue file i will write a code that cal call the controller and export the file
axios.get('api/export')
.then(()=>{
toast({
type: 'success',
title: 'Export the Data'
})
})
.catch(()=> {
toast({
type: 'warning',
title: 'Can not Export'
})
})
but the result is like that
that will return True, I really don't know how to solve this,plase help me
Upvotes: 2
Views: 12045
Reputation: 11
I faced the same issue when I was using Inertia, Laravel, or Vue project. But i found the solution.
exportData() {
// Construct the download URL with query parameters
const query = new URLSearchParams({
search: this.searchQuery,
sales_persons: this.searchSalesPerson,
products: this.searchProduct,
material_types: this.searchMaterial,
export: 'true'
}).toString();
// Trigger the browser to navigate to the download URL
window.location.href = this.route('module-certificate.index') + '?' + query;
},
Upvotes: 0
Reputation: 51
I think is a little bit late but I had the same problem and I just solved it.
For download a file from vuejs, you shouldn't make the request as an Axios petition (at least not that I know). Instead, you can use a href and download attribute inside your template.
This is an example:
<a
type="button"
href="/api/export/entity/"
download="file.xlsx"
>
<button
@click="export()"
class="btn btn-primary"
>
Export
</button>
</a>
The method helps if you need to do another stuff.
I hope this help you.
Upvotes: 3
Reputation: 321
ok I solve the problem, I use Vue-excel-export package that will Export the Excel File
this link can help me to install package and use it
Upvotes: 2