Magento 2自定义模块图片上传错误。文件未上传

时间:2019-02-08 12:49:47

标签: image file upload magento2

它是基于 magento-contact 的自定义表单。 Magento 2.3版。 问题在于文件成功上传

在\ Magento \ Framework \ File \ Uploader 构造函数中,我们可以看到:

if (!file_exists($this->_file['tmp_name'])) {
            $code = empty($this->_file['tmp_name']) ? self::TMP_NAME_EMPTY : 0;
            throw new \Exception('The file was not uploaded.', $code);
        }

然后在保存功能中:

try {
            $this->_result = $this->_moveFile($this->_file['tmp_name'], $destinationFile);
        } catch (\Exception $e) {
            // if the file exists and we had an exception continue anyway
            if (file_exists($destinationFile)) {
                $this->_result = true;
            } else {
                throw $e;
            }
        }

保存功能应在构造后执行,应该如此。并且文件上传并移动到“ requestForm /照片” 成功。但是,然后再构造一次渗透。这就是为什么没有 tmp文件的原因。因此,我的尝试失败了,提交请求表格后,我收到错误消息“ 文件未上传”。但是我不明白为什么。有什么想法吗?

Exception.log:

main.CRITICAL: The file was not uploaded. {"exception":"[object] (Exception(code: 0): The file was not uploaded. at /vendor/magento/framework/File/Uploader.php:175)"} []

这是我的控制器上传的图片:

        try{
            $uploaderFactory = $this->uploaderFactory->create(['fileId' => 'image']);
            $uploaderFactory->setAllowedExtensions(['jpg', 'jpeg', 'png']);
            $imageAdapter = $this->adapterFactory->create();
            $uploaderFactory->addValidateCallback('custom_image_upload',$imageAdapter,'validateUploadFile');
            $uploaderFactory->setAllowRenameFiles(true);
            $uploaderFactory->setFilesDispersion(true);
            $mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
            $destinationPath = $mediaDirectory->getAbsolutePath('requestForm/photo');
            $result = $uploaderFactory->save($destinationPath);
            if (!$result) {
                    throw new LocalizedException(
                            __('File cannot be saved to path: $1', $destinationPath)
                    );
            }
            $imagePath = 'requestForm/photo'.$result['file'];
            $data['photo'] = $imagePath;
        } catch (\Exception $e) {
            $this->messageManager->addError($e->getMessage());
        }

0 个答案:

没有答案