box / spout

Read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way
http://opensource.box.com/spout/
Apache License 2.0
4.23k stars 636 forks source link

net::ERR_INVALID_RESPONSE on attempting to download the Excel file in browser #416

Closed sivachaitanya closed 7 years ago

sivachaitanya commented 7 years ago

Hello,

I am new to Spout and downloaded the packages manually and placed them inside "Libraries" folder of my CodeIgniter project. My Controller looks like -

<?php
require_once (APPPATH.'libraries/Spout/Autoloader/autoload.php');
use Box\Spout\Writer\WriterFactory;
use Box\Spout\Common\Type;    
defined('BASEPATH') OR exit('No direct script access allowed');

class Download extends CI_Controller {

    public function __construct()
     {
        parent::__construct();
        $this->load->helper(array('url','language'));

    }

    public function index()
    {

        $data['child_content'] = 'download/index';
        $this->load->view('template/main',$data);
    }

    public function spoutdownload()
    {

       // $this->load->library('Spout/Writer/WriterFactory');
        //$this->load->library('Spout/Common/Type');
         $writer = WriterFactory::create(Type::XLSX); // for XLSX files
        //$writer = WriterFactory::create(Type::CSV); // for CSV files
        //$writer = WriterFactory::create(Type::ODS); // for ODS files

        //$writer->setTempFolder('/SpoutFiles');       
        //$writer->openToFile('php://output'); // write data to a file or to a PHP stream
        $writer->openToBrowser('Spout_output'); // stream data directly to the browser

        $writer->addRow(array("label1","label2")); // add a row at a time
        $writer->addRow(array("data1","data2")); // add multiple rows at a time

        $writer->close();

       exit;

    }

}

My view looks like -

<a href="<?php echo base_url(); ?>index.php/download/spoutdownload"> Download data in Excel format</a>

If I click the link in the browser it displays "Failed to load resource: net::ERR_INVALID_RESPONSE" in my browser console and I am unable to download the file. Can you please help me resolve the issue.

adrilo commented 7 years ago

You can try 2 things:

This is more a framework problem than a Spout problem here, so it's hard to help. Also, make sure that your spoutdownload is called!