本文介绍了Laravel Excel使用Controller下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我创建了一个PHP控制器来处理JS发布的导出数据.问题是我可以看到它在控制台中创建了某些内容,但是文件下载从未开始.我尝试使用-> store(laravel excel)并将其保存在导出文件夹中,但是当我尝试使用

So I created a PHP Controller to handle exporting data which is posted by JS. The problem is I can see it creates something in the console but the file download never starts. I tried using ->store (laravel excel) and keeping it in an export folder but again when I try to use

return \Response::download($result);

它仍然不会开始下载.我遇到的问题只是开始下载.

it still won't start the download. The problem I'm having is just getting the download to start.

角度控制器

$scope.exportMatrix = function () {
    var postData = {list: $scope.list, matrix: $scope.matrix};
    $http({
        method: 'POST',
        url: '/export',
        dataType: 'obj',
        data: postData,
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).success(function (data) {
        console.log(data);
    }).error(function (data) {
        console.log("failed");
    });
}

路线

Route::post('/export', 'ExportController@export');

PHP控制器

<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App;
use Excel;
use Response;
class ExportController extends Controller {

public function export()
{

    $excel = App::make('excel');

    Excel::create('Test', function($excel) {
        $excel->setTitle('new awesome title');

        $excel->sheet('Sheet', function($sheet) {
            $sheet->fromArray(array(
                array('data1', 'data2'),
                array('data3', 'data4')
            ));
        });


    })->export('xlsx');
}

推荐答案

最后,我使用了 FileSaver .js 下载它发送回的blob,因此只需加载FileSaver并使用saveAs(blob).

In the end I used FileSaver.js to download the blob it sent back so just load up FileSaver and use it saveAs(blob).

$http({
   method: 'POST',
   url: '/api/v1/download',
   dataType: 'json',
   data: {
       data:data
   },
   responseType: 'arraybuffer',
   headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function (data) {
   var blob = new Blob([data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});

   saveAs(blob, title + ".xlsx");
}).error(function (data) {
   console.log("failed");
});

我确保使用

和saveAs类型:

由于原因.

这篇关于Laravel Excel使用Controller下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 03:43