Angular 6 CSV下载

我是angular的新手,目前我正在一个需要csv导出的项目中工作。在这里,我使用Angular 6作为前端,使用laravel作为后端


这就是我使用mattwebsite / excel编写laravel函数的方式


// Lead export to csv

public function downloadExcel(Request $request)

{

    $credentials = $request->only('token');

    $token = $credentials['token'];

    $userid = $this->getUseridFromToken($token);

    $type = "xls";

    $data = DB::table('user_mailbox AS A')

                    ->select('A.id', 'A.name', 'A.email', 'A.phone', DB::raw('DATE_FORMAT(A.send_on, "%d / %b / %Y") as send_on'), 'B.listing_heading','B.listing_id','B.listing_heading', 'C.name')

                    ->leftjoin('broker_listing AS B', 'B.listing_id', '=', 'A.listing_id')

                    ->leftjoin('users AS C', 'C.id', '=', 'A.sent_by')

                    ->where('A.sent_to', $userid)

                    ->where('A.user_type', '1')

                    ->orderBy('A.id', 'desc')->get()->toArray(); 

    Excel::create('Lead_Export', function($excel) use ($data) {

        $excel->sheet('Lead_Export', function($sheet) use ($data)

        {

            $sheet->fromArray($data);

        });

    })->download($type);

}

这就是我在角度组件中编写函数的方式


    // Download leads as excel

download_excel(){

  const fd = new FormData(); 

  fd.append('token',this.token);

  this.brokerleads.downloadLeads(fd).subscribe(

    data => this.handleResponsedwnload(data),

    error => this.handleErrordwnload(error)

  );

}

handleResponsedwnload(data){ console.log('test');

  const blob = new Blob([data], { type: 'text/xls' });

  const url= window.URL.createObjectURL(blob);

  window.open(url);

}

handleErrordwnload(data){


}

服务就是这样


  // Download as excel

  downloadLeads(data):Observable<any>{

    return this.http.post(`${this.baseUrl}downloadExcel`, data);   

  }

看法


    <a class="export-leads" href="javascript:void(0);" (click)="download_excel()" >EXPORT LEADS</a>

在执行此操作时,我收到这样的响应,但文件未下载

http://img2.mukewang.com/6094d5680001789513660766.jpg

莫回无
浏览 172回答 3
3回答
打开App,查看更多内容
随时随地看视频慕课网APP