php - 图片上传错误“创建对象时发生类型错误”Magento 2.4.1
问题描述
在我的自定义表单中,我有一个选项图像上传。没有图像上传表单提交没有任何错误。但是当我用户图像上传代码并提交我的表单时出现错误
创建对象时发生类型错误:MKF\CustomApplication\Controller\Index\Submit\Interceptor,传递给 MKF\CustomApplication\Controller\Index\Submit::__construct() 的参数 4 必须实现接口 Magento\Framework\ObjectManagerInterface,Magento 实例MediaStorage\Model\File\UploaderFactory 给定,在第 14 行的 C:\xampp\htdocs\magento_new\generated\code\MKF\CustomApplication\Controller\Index\Submit\Interceptor.php 中调用
代码控制器Submit.php
namespace MKF\CustomApplication\Controller\Index;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use MKF\CustomApplication\Model\FormModel;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Action\Action;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
class Submit extends Action
{
protected $resultPageFactory;
protected $FormModel;
//file upload
protected $_objectManager;
protected $_storeManager;
protected $_filesystem;
protected $_fileUploaderFactory;
public function __construct(
Context $context,
PageFactory $resultPageFactory,
FormModel $FormModel,
\Magento\Framework\ObjectManagerInterface $objectManager,
StoreManagerInterface $storeManager,
\Magento\Framework\Filesystem $filesystem,
\Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory
)
{
$this->resultPageFactory = $resultPageFactory;
$this->FormModel = $FormModel;
$this->_objectManager = $objectManager;
$this->_storeManager = $storeManager;
$this->_filesystem = $filesystem;
$this->_fileUploaderFactory = $fileUploaderFactory;
parent::__construct($context);
}
public function execute()
{
// print_r($this->getRequest()->getPost());
// exit();
try {
$data = (array)$this->getRequest()->getPost();
//print_r($data); exit();
//print_r($file['owner_nid']['name']); exit();
$this->UploadImages();
if ($data) {
$model = $this->FormModel;
$model->setData($data);
$model->setData('owner_nid', $file['owner_nid']['name']);
$model->save();
$this->messageManager->addSuccessMessage(__("Data Saved Successfully."));
}
} catch (\Exception $e) {
$this->messageManager->addErrorMessage($e, __("We can\'t submit your request, Please try again."));
}
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$resultRedirect->setUrl($this->_redirect->getRefererUrl());
return $resultRedirect;
}
private function UploadImages(){
$files = $this->getRequest()->getFiles();
$mediaDir = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath();
$mediapath = $this->_mediaBaseDirectory = rtrim($mediaDir, '/');
if (isset($files['owner_nid']) && !empty($files['owner_nid']["name"])){
try {
$uploader = $this->_fileUploaderFactory->create(['fileId' => 'owner_nid']);
$uploader->setAllowedExtensions(['jpg', 'jpeg', 'gif', 'png']);
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(true);
$path = $mediapath . '/MKF/CustomApplication/';
$result = $uploader->save($path);
if (!$result) {
throw new LocalizedException(
__('File cannot be saved to path: $1', $path)
);
}
$imagePath = $path.$files['owner_nid']["name"];
}
catch (\Exception $e) {
$this->messageManager->addError(__($e->getMessage()));
}
//print_r($data['owner_nid'] = $imagePath); exit();
return $imagePath;
}
}
}
解决方案
我建议你切换到开发者模式并删除生成的文件夹
推荐阅读
- sql - 如何过滤sql server中的记录?
- python - 正则表达式仅在没有特定单词之前匹配字符
- javascript - selenium-webdriver chrome 代理认证
- excel - Windows 服务中的 Excel OLE 自动化
- typescript - 使用 lerna 和 typescript 为 webpack 配置别名
- python - 重置一个迭代器,它是一个地图对象?
- angular - 发送 Angular Post 调用时,Spring 中缺少所需的请求正文
- visual-studio-2019 - Visual Studio Windows Form App 在哪里调用 Form 构造函数
- php - 只能在运行测试时调试
- kaios - 如何在 KaiOS 应用中实现推送通知