2

我在这里遇到问题,我认为这基本上只是因为我错过了一步。当我保存文件时,图像确实保存到指定的目录。但是,当我去编辑表单时,我上传的图像没有显示。

这是我构建的第一个网格模块,所以我有点不清楚它是如何工作的。

在 Form.php 我的图像字段如下:

$fieldset->addField('something_else', 'image', array(
            'name'      => 'something_else[something_else]',
            'label'     => 'Something Else',
            'title'     => 'Something Else',
            'required'  => false,
        )); 

我已将 enctype 设置为:'enctype' => 'multipart/form-data'

我的控制器在 if ($postData = $this->getRequest()->getPost()) 之后的保存操作中有此代码:

if(isset($_FILES['something_else']))
                {
                    foreach($_FILES['something_else']['name'] as $imgname => $img)
                    {
                        if($_FILES['something_else']['name'][$imgname] != '' && isset($_FILES['something_else']['name'][$imgname]))
                        {
                            $imgArray = array(
                                'name' => $_FILES['something_else']['name'][$imgname],
                                'type' => $_FILES['something_else']['type'][$imgname],
                                'tmp_name' => $_FILES['something_else']['tmp_name'][$imgname],
                                'error' => $_FILES['something_else']['error'][$imgname],
                                'size' => $_FILES['something_else']['size'][$imgname]
                            );
                            $uploader = new Varien_File_Uploader($imgArray);
                            $uploader->setAllowedExtensions(array('jpg','jpeg','png','gif'));
                            $uploader->setAllowRenameFiles(true);
                            $uploader->setFilesDispersion(false);

                            $path = Mage::getBaseDir('media') . DS . 'somethingelse' . DS;

                            $name = str_replace(' ', '_', $_FILES['something_else']['name'][$imgname]);

                            $uploader->save($path, $name);

                            $bazData[$imgname] = 'somethingelse' . DS . $uploader->getCorrectFileName($name);
                        }
                        else
                        {
                            unset($bazData[$imgname]);
                        }
                    }
                }

在查看表单时,我还缺少什么将数据发送回 form.php 以显示吗?

我希望这是足够的信息,并提前致谢。

4

2 回答 2

1

试试这个和平的代码在您的图像字段表单中使用此代码:

 $fieldset->addField('filename', 'file', array(
          'label'     => Mage::helper('test')->__('File'),
          'required'  => false,
          'name'      => 'filename',
      ));

在控制器中的保存功能中使用此

if(isset($_FILES['filename']['name']) && $_FILES['filename']['name'] != '') { 
            try {   
                    /* Starting upload */   
                    $uploader = new Varien_File_Uploader('filename');

                    // Any extention would work
                    $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
                    $uploader->setAllowRenameFiles(false);

                    // Set the file upload mode 
                    // false -> get the file directly in the specified folder
                    // true -> get the file in the product like folders 
                    //  (file.jpg will go in something like /media/f/i/file.jpg)
                    $uploader->setFilesDispersion(false);

                    // We set media as the upload dir
                    $path = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA)."test/" ;
                    //echo $path, $_FILES['filename']['name'];exit;
                    $uploader->save($path, $_FILES['filename']['name'] );

                } catch (Exception $e) {

                }
          //  echo $path;exit;
                //this way the name is saved in DB

            $data['image_path'] = $path.$_FILES['filename']['name'];
        }

.希望它会帮助你

于 2013-04-24T04:03:39.833 回答
0

您必须在表单中设置字段值。喜欢:

$data = array('something_else' => 'image/url');
$fieldset->addField('something_else', 'image', array(
            'name'      => 'something_else',
            'label'     => 'Something Else',
            'title'     => 'Something Else',
            'required'  => false,
        )); 
$form->setValues($data);

或者:

$fieldset->addField('something_else', 'image', array(
            'name'      => 'something_else',
            'label'     => 'Something Else',
            'title'     => 'Something Else',
            'required'  => false,
            'value'     => 'image/url',
        )); 
于 2013-04-23T14:26:48.473 回答