For this \Magento\Framework\App\Response\Http\FileFactory
is your friend.
You can look into this class for more reference
\Magento\OfflineShipping\Controller\Adminhtml\System\Config\ExportTablerates
public function execute()
{
$fileName = 'tablerates.csv';
/** @var $gridBlock \Magento\OfflineShipping\Block\Adminhtml\Carrier\Tablerate\Grid */
$gridBlock = $this->_view->getLayout()->createBlock(
'Magento\OfflineShipping\Block\Adminhtml\Carrier\Tablerate\Grid'
);
$website = $this->_storeManager->getWebsite($this->getRequest()->getParam('website'));
if ($this->getRequest()->getParam('conditionName')) {
$conditionName = $this->getRequest()->getParam('conditionName');
} else {
$conditionName = $website->getConfig('carriers/tablerate/condition_name');
}
$gridBlock->setWebsiteId($website->getId())->setConditionName($conditionName);
$content = $gridBlock->getCsvFile();
return $this->_fileFactory->create($fileName, $content, DirectoryList::VAR_DIR);
}
Now, I think you can easily get an Idea how to crack your requirement :)