I need to create a file in mangento 2 admin.
I'm using a button to get in a controller route.
My form ui component:
<item name="buttons" xsi:type="array">
<item name="save" xsi:type="string">Vendor\Module\Block\Adminhtml\Report\Edit\ExportButton</item>
</item>
I used a dummy data provider just for use the submit url:
<dataSource name="bvz_report_specifier_form_data_source">
<argument name="dataProvider" xsi:type="configurableObject">
<argument name="class" xsi:type="string">Vendor\Module\Ui\Component\Form\DummyDataProvider</argument>
<argument name="name" xsi:type="string">bvz_report_specifier_form_data_source</argument>
<argument name="primaryFieldName" xsi:type="string">entity_id</argument>
<argument name="requestFieldName" xsi:type="string">entity_id</argument>
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="submit_url" xsi:type="url" path="*/*/export"/>
</item>
</argument>
</argument>
<argument name="data" xsi:type="array">
<item name="js_config" xsi:type="array">
<item name="component" xsi:type="string">Magento_Ui/js/form/provider</item>
</item>
</argument>
</dataSource>
In Vendor/Module/Controller/Adminhtml/Index/Export I creat an csv file and save it and put the name of file in URL.
Now I need to download this file when the page refresh.
In the index controller I have this code to make the download:
if($fileName = $this->getRequest()->getParam('file')){
$url = $mediaUrl = $this ->_storeManager-> getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA );
$resultRedirect = $this->resultRedirectFactory->create();
return $resultRedirect->setUrl($url.$fileName);
}