0

This is my code:

$fieldset->addField(
            'pdf_url',
            'file',
            ['name' => 'custom_url', 'label' => __('Custom Url'), 'title' => __('Custom Url'), 'required' => true]
        );

It's not show any value just show choose file.

0

You need a controller to upload your file.

{Vendor}/{Name}/Controller/{ControllerName}.php

<?php

namespace {Vendor}\{Name}\Controller;

use Magento\Backend\App\Action;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem;
use Magento\MediaStorage\Model\File\UploaderFactory;

class {ControllerName} extends Action
{
    protected $fileSystem;

    protected $uploaderFactory;

    protected $allowedExtensions = ['csv']; // Files allowed types 

    protected $fileId = 'file'; // name input file  

    public function __construct(
        Action\Context $context,
        Filesystem $fileSystem,
        UploaderFactory $uploaderFactory
    ) {
        $this->fileSystem = $fileSystem;
        $this->uploaderFactory = $uploaderFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        $destinationPath = $this->getDestinationPath();

        try {
            $uploader = $this->uploaderFactory->create(['fileId' => $this->fileId])
                ->setAllowCreateFolders(true)
                ->setAllowedExtensions($this->allowedExtensions)
                ->addValidateCallback('validate', $this, 'validateFile');
            if (!$uploader->save($destinationPath)) {
                throw new LocalizedException(
                    __('File cannot be saved to path: $1', $destinationPath)
                );
            }

            // process the uploaded file
        } catch (\Exception $e) {
            $this->messageManager->addError(
                __($e->getMessage())
            );
        }
    }

    public function validateFile($filePath)
    {
        // your custom validation code here
    }

    public function getDestinationPath()
    {
        return $this->fileSystem
            ->getDirectoryWrite(DirectoryList::TMP)
            ->getAbsolutePath('/');
    }
}
| improve this answer | |
  • i have done all every thing is good but not show value its save in db also – test May 4 '18 at 16:41
  • Where the value is not displayed ?, do you want to display it where and how ? please post your code – PЯINCƏ May 4 '18 at 16:45
  • when we edit from grid in magento2 backend – test May 4 '18 at 17:05
  • when we click on edit from grid and edit section open – test May 4 '18 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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