从 Laravel 下载 Excel 文件的返回数据是无意义的,如何解决?
Returned data from Laravel for Downloading Excel file is nonsense, How to fix it?
我有一个项目,我在 blade 文件中使用 vue.js,为了导出 excel,我正在尝试这种方法,它给出了这样的响应:
代码结构如下:
在 Blade.php(有一个按钮,它正在获取 startDate、endData 以获取结果:
<button v-if="db_query!=null" @click="save_excel()" id="send" type="button" class="btn btn-success">Export</button>
save_excel:function(){
let self=this;
let start_at=$('#start_at').val();
let end_at=$('#end_at').val();
$.ajax({
type: "post",
url: "{{route('report.save_as_excel')}}",
data: {
_token:"{{csrf_token()}}",
name :self.name,
created_at :self.created_at,
file_id :self.file_id,
order :self.order,
paid_price :self.paid_price,
phone :self.phone,
price :self.price,
products :self.products,
products_desc :self.products_desc,
products_order_desc :self.products_order_desc,
reagents :self.reagents,
status :self.status,
time_id :self.time_id,
unpaid_price :self.unpaid_price,
check_box :self.check_box,
'end_at' :end_at,
'start_at' :start_at,
},
success: function (response) {
self.result=response.customers;
console.log(response);
window.open(response);
}
});
},
CustomerAtlasExports.php
class CustomerAtlasExports implements FromQuery, Responsable
{
use Exportable;
public $start;
public $end;
public $where;
private $fileName = 'Gozaresh.xlsx';
public function __construct($start,$end,$where)
{
$this->start = $start;
$this->end = $end;
$this->where = $where;
}
public function query()
{
return Customer::with(['products','reagents'])->whereBetween('created_at',[$this->start,$this->end])->where($this->where)->get();;
}
}
Controller.php :
$where=$this->c_query_builder();
$start=Carbon::createFromTimestamp(substr($request->start_at,0,10))->hour(0)->minute(0)->second(0);
$end=Carbon::createFromTimestamp(substr($request->end_at,0,10))->hour(23)->minute(59)->second(59);
return (new CustomerAtlasExports($start,$end,$where));
在他们解释的文档中,我应该获取文件来下载它,我还尝试在控制器中使用 ->Download
而不是在导出文件中使用 Responsable。
您无法通过 AJAX 下载文件。数据最终在您网页的 JavaScript 变量中,而不是磁盘上的文件。 (好吧,有时候有一种方法可以用一些 JavaScript 来捏造它,但不推荐这样做,而且可能并不总是有效)。
如果您想通过 JavaScript 启动下载,通常的方法是使用 window.open() 或 window.location 在浏览器直接导致下载发生。然后它通过常规请求而不是 ajax 完成,并以您期望的方式处理。
在您的情况下,如果您要发布要转换为 Excel 文件的数据,我会将 PHP return 设为 URL 作为响应AJAX 请求。 URL 将指向 Excel 文件在服务器上的保存位置。然后在AJAX请求代码的"success"回调中,使用JavaScript(如上所述)告诉浏览器去访问那个URL.
N.B。您可能还需要考虑 cron 作业或服务器上的某些东西,以便在一段时间后整理旧的 Excel 文件。或许您可以指导用户,在您删除文件之前,他们的下载 URL 仅在一定的小时数或天数内有效。这样你的磁盘就不会装满旧的垃圾文件了。
我有一个项目,我在 blade 文件中使用 vue.js,为了导出 excel,我正在尝试这种方法,它给出了这样的响应:
代码结构如下:
在 Blade.php(有一个按钮,它正在获取 startDate、endData 以获取结果:
<button v-if="db_query!=null" @click="save_excel()" id="send" type="button" class="btn btn-success">Export</button>
save_excel:function(){
let self=this;
let start_at=$('#start_at').val();
let end_at=$('#end_at').val();
$.ajax({
type: "post",
url: "{{route('report.save_as_excel')}}",
data: {
_token:"{{csrf_token()}}",
name :self.name,
created_at :self.created_at,
file_id :self.file_id,
order :self.order,
paid_price :self.paid_price,
phone :self.phone,
price :self.price,
products :self.products,
products_desc :self.products_desc,
products_order_desc :self.products_order_desc,
reagents :self.reagents,
status :self.status,
time_id :self.time_id,
unpaid_price :self.unpaid_price,
check_box :self.check_box,
'end_at' :end_at,
'start_at' :start_at,
},
success: function (response) {
self.result=response.customers;
console.log(response);
window.open(response);
}
});
},
CustomerAtlasExports.php
class CustomerAtlasExports implements FromQuery, Responsable
{
use Exportable;
public $start;
public $end;
public $where;
private $fileName = 'Gozaresh.xlsx';
public function __construct($start,$end,$where)
{
$this->start = $start;
$this->end = $end;
$this->where = $where;
}
public function query()
{
return Customer::with(['products','reagents'])->whereBetween('created_at',[$this->start,$this->end])->where($this->where)->get();;
}
}
Controller.php :
$where=$this->c_query_builder();
$start=Carbon::createFromTimestamp(substr($request->start_at,0,10))->hour(0)->minute(0)->second(0);
$end=Carbon::createFromTimestamp(substr($request->end_at,0,10))->hour(23)->minute(59)->second(59);
return (new CustomerAtlasExports($start,$end,$where));
在他们解释的文档中,我应该获取文件来下载它,我还尝试在控制器中使用 ->Download
而不是在导出文件中使用 Responsable。
您无法通过 AJAX 下载文件。数据最终在您网页的 JavaScript 变量中,而不是磁盘上的文件。 (好吧,有时候有一种方法可以用一些 JavaScript 来捏造它,但不推荐这样做,而且可能并不总是有效)。
如果您想通过 JavaScript 启动下载,通常的方法是使用 window.open() 或 window.location 在浏览器直接导致下载发生。然后它通过常规请求而不是 ajax 完成,并以您期望的方式处理。
在您的情况下,如果您要发布要转换为 Excel 文件的数据,我会将 PHP return 设为 URL 作为响应AJAX 请求。 URL 将指向 Excel 文件在服务器上的保存位置。然后在AJAX请求代码的"success"回调中,使用JavaScript(如上所述)告诉浏览器去访问那个URL.
N.B。您可能还需要考虑 cron 作业或服务器上的某些东西,以便在一段时间后整理旧的 Excel 文件。或许您可以指导用户,在您删除文件之前,他们的下载 URL 仅在一定的小时数或天数内有效。这样你的磁盘就不会装满旧的垃圾文件了。