在Magento 2中以自定义形式上传csv时,错误为“文件未上传”

时间:2018-04-12 15:58:56

标签: magento2 magento-2.0 magento2.1 magento2.2 magento-2.2.1

我习惯使用以下代码在Magento 2中上传自定义表格的表格。但是收到错误

  

文件未上传

<?php
namespace Auction\Bid\Controller\UploadPo;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem;
use Magento\MediaStorage\Model\File\UploaderFactory;
class Index extends \Magento\Framework\App\Action\Action
{ 

    protected $uploaderFactory;
    protected $fileSystem;
    protected $fileId = 'purchaseorder_po_file';
    protected $allowedExtensions = ['csv', 'pdf'];

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

    public function execute(){
        if (isset($_FILES['file']['name']) && $_FILES['file']['name'] != "") {
            $destinationPath = $this->getDestinationPath();
            try {
                $uploader = $this->uploaderFactory->create(['fileId' => $this->fileId])
                    ->setAllowCreateFolders(true)
                    ->setAllowRenameFiles(true)
                    ->setAllowedExtensions($this->allowedExtensions);

                if (!$fileData = $uploader->save($destinationPath)) {
                    throw new LocalizedException(
                        __('File cannot be saved to path: $1', $destinationPath)
                    );
                }
                //$fileName = $fileData['file'];
            } catch (\Exception $e) {
                $message = $this->messageManager->addError(
                    __($e->getMessage())
                );
                die;
            }
        } else{
            die('there');
        }

}
public function getDestinationPath()
    {
        return $this->fileSystem
            ->getDirectoryWrite(DirectoryList::MEDIA)
            ->getAbsolutePath('/');
    }
}

这里有什么问题?

0 个答案:

没有答案