/** * Save uploaded file before saving config value * * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ public function beforeSave() { $value = $this->getValue(); $tmpName = $this->_requestData->getTmpName($this->getPath()); $file = []; if ($tmpName) { $file['tmp_name'] = $tmpName; $file['name'] = $this->_requestData->getName($this->getPath()); } elseif (!empty($value['tmp_name'])) { $file['tmp_name'] = $value['tmp_name']; $file['name'] = $value['value']; } 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(__($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(''); } else { $this->unsValue(); } } return $this; }
/** * Receiving uploaded file data * * @return array */ protected function getFileData() { $file = []; $value = $this->getValue(); $tmpName = $this->_requestData->getTmpName($this->getPath()); if ($tmpName) { $file['tmp_name'] = $tmpName; $file['name'] = $this->_requestData->getName($this->getPath()); } elseif (!empty($value['tmp_name'])) { $file['tmp_name'] = $value['tmp_name']; $file['name'] = isset($value['value']) ? $value['value'] : $value['name']; } return $file; }