1

I am creating a module for check distance from store to various area. For this, I need to give sample csv file format to store managers so they can format their csv files and upload in admin-side.

I need to put that link besides file-browse button.

For that, I have created below code.

$fieldset->addField(
        'csv_file',
        'file',
        [
            'name'  => 'csv_file',
            'label' => __('Upload Csv File'),
            'title' => __('Upload Csv File'),
            'note' => '<br><span id="sample-file-span" 
                                    . '<a id="sample-file-link" href="#">'
                                    . __('Download Sample File')
                                    . '</a></span>',
        ]
    );

By this, I can see the link.

My questions are,

Where I put my sample csv file in my module ?

What to write code in href attribute so store-manager can download it ?

Advanced Thanks !

0

1 Answer 1

2

You need to create custom type in form

$fieldset->addType('csvfile', '\Vendor\Module\Block\Adminhtml\Youblock\Renderer\Csvfile');

$fieldset->addField(
    'file',
    'csvfile',
    [
        'name'  => 'csvfile',
        'label' => __('csvfile'),
        'title' => __('csvfile'),

    ]
);

app/code/Vendor/Module/Block/Adminhtml/Yourblock/Renderer/Csvfile.php

<?php

namespace Vendor\Module\Block\Adminhtml\Yourblock\Renderer;

use Magento\Framework\DataObject;

class Csvfile extends \Magento\Framework\Data\Form\Element\AbstractElement
{
    protected $_assetRepo;

    public function __construct( 
         \Magento\Framework\View\Asset\Repository $assetRepo
    ) {
         $this->_assetRepo = $assetRepo;
    }

    public function getElementHtml()
    {
         $csvFile = $this->_assetRepo->getUrl('Vendor_Module::csv/yourfile.csv');
         $csvLink = "<a href=".$csvFile." target='_blank'>Download Sample File</a>";
        return $csvLink;
    }

}

Copy yourfile.csv at

app/code/Vendor/Module/view/adminhtml/web/csv/yourfile.csv

3
  • Thank you so much @Prince Patel ! Its working exactly what I want :)
    – Prince
    Commented May 12, 2017 at 10:04
  • @Prince how can I get the value of file element in renderer?
    – Rajan Soni
    Commented Mar 7, 2018 at 12:49
  • @Rajan Soni $this->getValue(); used these get value of element. Commented Jul 14, 2018 at 6:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.