Skip to content

Why I am unable to submit the form in adminhtml using custom block in publichtml?

Instead of edit page I am using a simple file upload form using a block:

upload_form.phtml

<form method="POST" action="<?=$block->getUrl("/test/uploadcsv");?>" enctype="multipart/form-data">
    <input id="emailcsv_uploadform" type="file" name="upload_csv">
    <button type="submit">Upload File</button>
</form>

That is loaded from this template:

<?xml version="1.0" ?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="styles"/>
    <body>
        <referenceContainer name="content">
            <block  name="pcmagas.test.uploadform" class="PcmagasTestBlockAdminhtmlUploadForm" template="upload_form.phtml"/>
        </referenceContainer>
    </body>
</page>
namespace PcmagasTestBlockAdminhtml;

use MagentoFrameworkViewElementTemplate;

class UploadForm extends Template
{

}

And the controller that I use to handle the upload is the following:

namespace PcmagasTestControllerAdminhtmlTest;

use MagentoFrameworkAppActionHttpPostActionInterface;
use MagentoFrameworkAppResponseInterface;
use MagentoFrameworkControllerResultFactory;
use MagentoFrameworkControllerResultInterface;
use MagentoFrameworkExceptionNotFoundException;

class Uploadcsv extends MagentoBackendAppAction
{
    /**
     * Constructor
     *
     * @param MagentoBackendAppActionContext $context
     */
    public function __construct(
        MagentoBackendAppActionContext $context
    ) {
        parent::__construct($context);
    }

    /**
     * Execute action based on request and return result
     *
     * @return ResultInterface|ResponseInterface
     * @throws NotFoundException
     */
    public function execute()
    {
        $data = $this->getRequest()->getFiles();
        die("Psofisa"); // Debug code

        return "LALALA";
    }
}

Using the following etc/adminhtml/routes.xml:

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route frontName="csvupload" id="csvupload">
            <module before="Magento_Backend" name="Pcmagas_Test"/>
        </route>
    </router>
</config>

But whilst I submit the form I am redirected into magento’s adminhtml frontpage instead of breaking the submition upon the die.

Do you have any idea why?