带有图像文件的Magento自定义管理模块

时间:2015-06-25 07:19:14

标签: php magento

我正在尝试在Magento中创建一个管理模块。

第1步: 其中包含以下字段

enter image description here

第2步:填写一些值和图像

enter image description here

步骤3:当我尝试保存此项目时,项目已成功保存,但此处未显示图像。但实际上这些图像是在我的Magento Media文件夹中复制的。

步骤4:项目成功保存后,显示如下

enter image description here

但实际上我希望它能显示出类似的东西

enter image description here

这是我的代码请找到它。

_prepareForm() - 字段集代码:

$fieldset->addField('image', 'file', array(
    'label'     => Mage::helper('modulename')->__('Image'),
    'name'      => 'image',
    'note' => '(*.jpg, *.png, *.gif)',
));

_prepareForm()

<?php
class Namespace_ModuleName_Block_Adminhtml_Measurement_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
  protected function _prepareForm()
  {
      $form = new Varien_Data_Form(array(
                                      'id' => 'edit_form',
                                      'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
                                      'method' => 'post',
                                      'enctype' => 'multipart/form-data'
                                   )
      );
      $form->setUseContainer(true);
      $this->setForm($form);
      return parent::_prepareForm();
  }
}
?>

saveAction()

public function saveAction() {
    if ($data = $this->getRequest()->getPost()) {
        $model = Mage::getModel('modulename/modulename');
        $model->setData($data)->setId($this->getRequest()->getParam('id'));
        try {
            if ($model->getCreatedTime == NULL || $model->getUpdateTime() == NULL) {
                $model->setCreatedTime(now())
                    ->setUpdateTime(now());
            } else {
                $model->setUpdateTime(now());
            }
            $model->save();
            if(isset($_FILES['image']['name']) and (file_exists($_FILES['image']['tmp_name']))) {
                try {
                    $uploader = new Varien_File_Uploader('image');
                    $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png')); // or pdf or anything
                    $uploader->setAllowRenameFiles(false);
                    // setAllowRenameFiles(true) -> move your file in a folder the magento way
                    // setAllowRenameFiles(true) -> move your file directly in the $path folder
                    $uploader->setFilesDispersion(false);
                    $path = Mage::getBaseDir('media') . '/modulename_images/' ;
                    $uploader->save($path, $model->getId().'.jpg');
                    $model->setImage($model->getId().'.jpg');
                    $model->save();
                }catch(Exception $e) {
                    print_r($e);
                    die;
                }
            }
            else {       
                if(isset($data['image']['delete']) && $data['image']['delete'] == 1)
                $data['image_main'] = '';
                else
                unset($data['image']);
            }

            // Mage::getModel('modulename/flatrates')->saveMultipleFlatrates($data, $model->getId(),$this->getRequest()->getParam('cat_id'));
            Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('modulename')->__('Item was successfully saved'));
            Mage::getSingleton('adminhtml/session')->setFormData(false);
            if ($this->getRequest()->getParam('back')) {
                $this->_redirect('*/*/edit', array('id' => $model->getId()));
                return;
            }
            $this->_redirect('*/*/');
            return;
        } catch (Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            Mage::getSingleton('adminhtml/session')->setFormData($data);
            $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
            return;
        }
    }
    Mage::getSingleton('adminhtml/session')->addError(Mage::helper('modulename')->__('Unable to find item to save'));
    $this->_redirect('*/*/');
}

我在这里做错了什么?

任何想法?

如果有任何法术错误,我真的很抱歉。

2 个答案:

答案 0 :(得分:2)

为了获得图像预览,您必须使用image字段类型,而不是file,如下所示(查看第二个参数):

$fieldset->addField('image', 'image', array(
    'label'     => Mage::helper('modulename')->__('Image'),
    'name'      => 'image',
    'note' => '(*.jpg, *.png, *.gif)',
));

答案 1 :(得分:0)

这是因为您已将图像存储在媒体文件夹的子目录中,因此您必须在显示之前设置图像字段的值()。 比如在EditAction()中添加此代码,这是我的字段名称profile_pic

if($model->getProfilePic())
{
  $model->setProfilePic('testimonial/'.$model->getProfilePic());
}

或者您可以使用帮助器预览该图像