Skip to content

Upload pdf threw ui components

I’m trying to upload a pdf in custom form, but i’m failing. My controller doesn’t seems to receive the data.

    <field name="pdf" sortOrder="100" formElement="fileUploader">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="source" xsi:type="string">booklet</item>
            </item>
        </argument>
        <settings>
            <elementTmpl>ui/form/element/uploader/uploader</elementTmpl>
            <dataType>string</dataType>
            <label translate="true">Booklet Pdf</label>
            <visible>true</visible>
            <required>false</required>
        </settings>
        <formElements>
            <fileUploader>
                <settings>
                    <previewTmpl>Sesa_Booklet/image-preview</previewTmpl>
                    <required>true</required>
                    <uploaderConfig>
                        <param xsi:type="url" name="url" path="booklet/pdf/upload"/>
                    </uploaderConfig>
                    <allowedExtensions>pdf</allowedExtensions>
                    <maxFileSize>4194304</maxFileSize>
                </settings>
            </fileUploader>
        </formElements>
    </field>

On the save controller; this is null

        $pdfParam = $this->_request->getParam('pdf');

I followed the image way to try it

<virtualType name="SesaBookletFileUpload" type="SesaBookletModelFileUploader">
    <arguments>
        <argument name="baseTmpPath" xsi:type="string">booklet_file/upload</argument>
        <argument name="basePath" xsi:type="string">booklet_file/upload</argument>
        <argument name="allowedExtensions" xsi:type="array">
            <item name="pdf" xsi:type="string">pdf</item>
        </argument>
    </arguments>
</virtualType>
<type name="SesaBookletControllerAdminhtmlPdfUpload">
    <arguments>
        <argument name="fileUploader" xsi:type="object">SesaBookletFileUpload</argument>
    </arguments>
</type>

And this is my file upload

/**
 * Upload file controller action
 *
 * @return MagentoFrameworkControllerResultInterface
 */
public function execute()
{
    try {
        $result = $this->fileUploader->saveFileToTmpDir('pdf');

        $result['cookie'] = [
            'name' => $this->_getSession()->getName(),
            'value' => $this->_getSession()->getSessionId(),
            'lifetime' => $this->_getSession()->getCookieLifetime(),
            'path' => $this->_getSession()->getCookiePath(),
            'domain' => $this->_getSession()->getCookieDomain(),
        ];
    } catch (Exception $e) {
        $result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
    }
    return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($result);
}