public function beforeSave()
    {
        $value = $this->getValue();
        $file = $this->getFileData();
        if (!empty($file)) {
            $uploadDir = $this->_getUploadDir();
            try {
                $uploader = $this->_uploaderFactory->create(['fileId' => $file]);
                $uploader->setAllowedExtensions($this->getAllowedExtensions());
                $uploader->setAllowRenameFiles(true);
                $uploader->addValidateCallback('size', $this, 'validateMaxSize');
                $result = $uploader->save($uploadDir);
            } catch (\Exception $e) {
                throw new \Magento\Framework\Exception\LocalizedException(__('%1', $e->getMessage()));
            }

            $filename = $result['file'];
            if ($filename) {
                if ($this->_addWhetherScopeInfo()) {
                    $filename = $this->_prependScopeInfo($filename);
                }
                $this->setValue($filename);
            }
        } else {
            if (is_array($value) && !empty($value['delete'])) {
                $this->setValue('');
            } elseif (is_array($value) && !empty($value['value'])) {
                $this->setValue($value['value']);
            } else {
                $this->unsValue();
            }
        }

        return $this;
    }
public function beforeSave()
    {
        $value = $this->getValue();
        $file = $this->getFileData();
        if (!empty($file)) {
            $uploadDir = $this->_getUploadDir();
            try {
                $uploader = $this->_uploaderFactory->create(['fileId' => $file]);
                $uploader->setAllowedExtensions($this->getAllowedExtensions());
                $uploader->setAllowRenameFiles(true);
                $uploader->addValidateCallback('size', $this, 'validateMaxSize');
                $result = $uploader->save($uploadDir);
            } catch (\Exception $e) {
                throw new \Magento\Framework\Exception\LocalizedException(__('%1', $e->getMessage()));
            }

            $filename = $result['file'];
            if ($filename) {
                if ($this->_addWhetherScopeInfo()) {
                    $filename = $this->_prependScopeInfo($filename);
                }
                $this->setValue($filename);
            }
        } else {
            if (is_array($value) && !empty($value['delete'])) {
                $this->setValue('');
            } elseif (is_array($value) && !empty($value['value'])) {
                $this->setValue($value['value']);
            } else {
                $this->unsValue();
            }
        }

        return $this;
    }
public function beforeSave ( ) { $ value = $ this -> getValue ( ) ; $ file = $ this -> getFileData ( ) ; if ( ! empty ( $ file ) ) { $ uploadDir = $ this -> _getUploadDir ( ) ; try { $ uploader = $ this -> _uploaderFactory -> create ( [ 'fileId' => $ file ] ) ; $ uploader -> setAllowedExtensions ( $ this -> getAllowedExtensions ( ) ) ; $ uploader -> setAllowRenameFiles ( true ) ; $ uploader -> addValidateCallback ( 'size' , $ this , 'validateMaxSize' ) ; $ result = $ uploader -> save ( $ uploadDir ) ; } catch ( \ Exception $ e ) { throw new \ Magento \ Framework \ Exception \ LocalizedException ( __ ( '%1' , $ e -> getMessage ( ) ) ) ; } $ filename = $ result [ 'file' ] ; if ( $ filename ) { if ( $ this -> _addWhetherScopeInfo ( ) ) { $ filename = $ this -> _prependScopeInfo ( $ filename ) ; } $ this -> setValue ( $ filename ) ; } } else { if ( is_array ( $ value ) && ! empty ( $ value [ 'delete' ] ) ) { $ this -> setValue ( '' ) ; } elseif ( is_array ( $ value ) && ! empty ( $ value [ 'value' ] ) ) { $ this -> setValue ( $ value [ 'value' ] ) ; } else { $ this -> unsValue ( ) ; } } return $ this ; }
Save uploaded file before saving config value

@return $this
@throws \Magento\Framework\Exception\LocalizedException
Save uploaded file before saving config value