Magento 2.2.5:图像未使用自定义ui组件文件上传器保存在数据库中

时间:2018-09-26 09:19:29

标签: php magento magento2

因此,我遵循了这个问题的答案(https://magento.stackexchange.com/questions/184731/magento-2-implement-ui-fileuploader),但是由于某些原因,当我通过编辑产品页面上传图片并点击保存时,图片没有保存,因此我使用记录仪登录到如果Save.php执行而文件却没有执行,我似乎无法找出为什么它没有被执行。根据所提问题的答案,据我所知,步骤1-5的所有操作均按预期工作,但上载的图像未保存在数据库中。

这是我的 Save.php控制器

<?php

namespace ImageUploader\Image\Controller\Adminhtml;

use Magento\Framework\Exception\LocalizedException;

class Save extends \Magento\Backend\App\Action
{
    protected $dataPersistor;

    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\App\Request\DataPersistorInterface $dataPersistor
) {
    $this->dataPersistor = $dataPersistor;
    parent::__construct($context);
}

    public function execute()
    {

        // logger logging into a file if file is executed
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info('working');

        $data = $this->_filterFoodData($data);
        $model->setData($data);
        $model->save(); 
     }

     public function _filterFoodData(array $rawData)
     {
         //Replace icon with fileuploader field name
         $data = $rawData;
         if (isset($data['image'][0]['name'])) {
             $data['image'] = $data['image'][0]['name'];
         } else {
             $data['image'] = null;
         }
         return $data;
    }
}

这是我的 DataProvider.php模型

<?php

namespace ImageUploader\Image\Model;

use Magento\Store\Model\StoreManagerInterface;

class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{
    public function getData()
    {
        $items = $this->collection->getItems();

        //Replace icon with fileuploader field name
       foreach ($items as $model) {
           $this->loadedData[$model->getId()] = $model->getData();
           if ($model->getIcon()) {
               $m['image'][0]['name'] = $model->getIcon();
               $m['image'][0]['url'] = $this->getMediaUrl().$model->getIcon();
               $fullData = $this->loadedData;
               $this->loadedData[$model->getId()] = array_merge($fullData[$model->getId()], $m);
        }
    }

    return $this->loadedData;
}

    public function getMediaUrl()
    {
        $mediaUrl = $this->storeManager->getStore()
        ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA).'imageuploader/tmp/image/';
        return $mediaUrl;
    }
}

当我检查请求的URL时,它说“ $ _FILES数组为空,错误代码:0”,该图像上传显示了它的预览,但是当我点击保存时,它消失了,并且Save.php文件未记录任何内容。有人可以帮助我,如果您需要更多信息,请告诉我。

0 个答案:

没有答案