从Laravel返回的用于下载Excel文件的数据是胡说八道,该如何解决?

时间:2019-03-21 13:40:00

标签: php laravel laravel-excel

我有一个项目,我在刀片文件中使用vue.js,并且为了导出excel,我正在尝试这种方法,它给出了这样的响应:

enter image description here

这是代码结构:

在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而不是在导出文件中使用负责人。

2 个答案:

答案 0 :(得分:1)

您无法通过AJAX下载文件。数据最终显示在网页上的JavaScript变量中,而不是磁盘上的文件中。 (有时候,有一种方法可以用一些JavaScript来修饰它,但是不建议使用它,并且可能并不总是有效的)。

如果要通过JavaScript发起下载,通常的方法是使用window.open()或window.location在浏览器中直接打开一个导致下载发生的新URL。然后通过常规请求(而不是ajax)完成操作,并按照您期望的方式进行处理。

在您的情况下,如果要发布要转换为Excel文件的数据,我将使PHP返回一个URL作为对AJAX请求的响应。该URL将指向Excel文件在服务器上的保存位置。然后,在AJAX请求代码的“成功”回调中,使用JavaScript(如上所述)告诉浏览器访问该URL。

您可能还需要考虑cron作业或服务器上的某些内容,以在一段时间后整理旧的Excel文件。也许您可以向用户提供指导,使他们的下载URL在删除文件之前仅在一定小时或几天内有效。这样您的磁盘就不会装满旧的垃圾文件。

答案 1 :(得分:-1)

好吧,面对相同的问题之后,事实证明@ADyson是正确的。以我为例,我是直接从Vue组件导出的,它会使用Axios将这些符号直接返回到控制台,但是如果您从链接中触发“导出”操作,则效果很好。只需确保Axios将数据发送到您的Laravel控制器即可。祝你好运。