如何在magento 2中添加自定义文件上传功能

问题描述 投票:2回答:3

我通过Magento 2类别tab中的自定义模块创建了文件上传功能。我不知道如何将文件保存在database.please中是否可以告诉我该怎么做?

magento magento2 magento-2.0 magento2.0.2
3个回答
1
投票
add use Magento\Framework\UrlInterface;
add use Magento\Framework\Filesystem;

...
$imageName = $this->uploadFileAndGetName('input_name', $this->fileSystem->getDirectoryWrite(DirectoryList::MEDIA)->getAbsolutePath($subdir_of_your_choice.'/image'));
$your_model->setImage($imageName);

0
投票

您不将文件保存在DB中,只保存名称:

    public function __construct(
    ....
    \Magento\Framework\File\UploaderFactory $uploaderFactory,
    ...
) {
    ......
    $this->uploaderFactory = $uploaderFactory;
    .....
}

public function uploadFileAndGetName($input, $destinationFolder)
{
    try {

            $uploader = $this->uploaderFactory->create(array('fileId' => $input));
            /** test The File with Callback here */
            $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
            $uploader->setAllowRenameFiles(true);
            $uploader->setFilesDispersion(true);
            $uploader->setAllowCreateFolders(true);
            $result = $uploader->save($destinationFolder);
            return $result['file'];

    } catch (\Exception $e) {             
        if ($e->getCode() != \Magento\Framework\File\Uploader::TMP_NAME_EMPTY) {
            throw new FrameworkException($e->getMessage());
        }
    }
    return '';
}

0
投票

以下代码是在自定义表中上传文件,

namespace MODULE\NAMESPACE\Controller\Index;
use Magento\Framework\App\Filesystem\DirectoryList;
class NAMESPACE extends \Magento\Framework\App\Action\Action
{
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
        \Magento\Framework\Message\ManagerInterface $messageManager
        ) {
        $this->_customerSession = $customerSession;
        $this->resultJsonFactory = $resultJsonFactory;
        $this->messageManager = $messageManager;
        parent::__construct($context);
    }
    public function execute()
    {
        $params =  $this->getRequest()->getParams();
        $result = $this->resultJsonFactory->create();
        $resultRedirect = $this->resultRedirectFactory->create();
        if ($params) {
            $data['name'] = $params['name'];
            $data['email_id'] = $params['email'];
            $data['city'] = isset($params['city'])? $params['city'] : '';
            $data['store_id'] = $params['storeid'];

            $model = $this->_objectManager->create('MODEL FILE PATH of specific table');
            try{
                $uploader = $this->_objectManager->create(
                    'Magento\MediaStorage\Model\File\Uploader',
                    ['fileId' => 'FIELDNAME']
                );
                $uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
                /** @var \Magento\Framework\Image\Adapter\AdapterInterface $imageAdapter */
                $imageAdapter = $this->_objectManager->get('Magento\Framework\Image\AdapterFactory')->create();
                $uploader->setAllowRenameFiles(true);
                $uploader->setFilesDispersion(true);
                /** @var \Magento\Framework\Filesystem\Directory\Read $mediaDirectory */
                $mediaDirectory = $this->_objectManager->get('Magento\Framework\Filesystem')
                    ->getDirectoryRead(DirectoryList::MEDIA);
                $result = $uploader->save($mediaDirectory->getAbsolutePath('FOLDERNAME'));
                    if($result['error']==0)
                    {
                        $data['offline_bill'] = 'FOLDERNAME' . $result['file'];
                    }
            } catch (\Exception $e) {

            }
            $model->setData($data);            
            try
            {
                $model->save();
                $this->messageManager->addSuccess(__('The form submitted successfully.'));
            }
            catch (\Magento\Framework\Exception\LocalizedException $e) 
            {
                $this->messageManager->addError($e->getMessage());
            }
            return $resultRedirect->setPath(REDIRECT PATH);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.