Pages

15 April, 2022

CSV File Download Programmatically in Magento 2

 CSV File Download Programmatically in Magento 2

Please check the below code,

<?php
namespace Webkul\Csv\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Filesystem\DirectoryList;

class GetFile extends Action
{
    public function __construct(
        Context $context,
        \Magento\Framework\App\Response\Http\FileFactory $fileFactory
    ) {
        $this->fileFactory = $fileFactory;
        parent::__construct($context);
    }
 
    public function execute()
    {
        $filepath = 'export/customerlist.csv';
        $downloadedFileName = 'CustomerList.csv';
        $content['type'] = 'filename';
        $content['value'] = $filepath;
        $content['rm'] = 1;
        return $this->fileFactory->create($downloadedFileName, $content, DirectoryList::VAR_DIR);
    }
}

Hi Guys! if you want to create CSV file programmatically in Magento 2 . Create a CSV file in Magento 2



No comments:

Post a Comment

Thanks