问题描述
我遵循,在我的Magento中进行图片上传模块,但所有代码所做的最大的事情是通过POST传递选定的文件名: $ _ FILES
数组是空的,只要我添加 image $ c
<?php
class Moo_News_Block_Adminhtml_News_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{
$ form = new Varien_Data_Form();
$ this-> setForm($ form);
$ fieldset = $ form-> addFieldset('news_form',array('legend'=> Mage :: helper('news') - > __('Item information')));
$ fieldset-> addField('title','text',array(
'label'=> Mage :: helper('news') - > __(' Title'),
'class'=>'required-entry',
'required'=> true,
'name'=>'title',
));
$ fieldset-> addField('published','select',array(
'label'=> Mage :: helper('news') - > __('
'name'=>'published',
'required'=> true,
'values'=> array(
array(
'value'=> 1,
'label'=> Mage :: helper('news') - &__('Yes'),
),
array(
'value'=> 0,
'label'=> Mage :: helper('news') - > __('No'),
),
),
));
$ fieldset-> addField('short_description','editor',array(
'name'=>'short_description',
'label'=>法师:: helper('news') - > __('Short Description'),
'title'=> Mage :: helper('news') - & b $ b'style'=>'width:98%; height:200px;',
'wysiwyg'=> false,
'required'=> true,
) );
$ fieldset-> addField('body','editor',array(
'name'=>'body',
'label'=>法师:: helper('news') - > __('Body'),
'title'=> Mage :: helper('news') - &__('Body'),
'style'=>'width:98%; height:400px;',
'wysiwyg'=> false,
'required'=> true,
));
$ b $ fieldset-> addField('image','file',array(
'label'=> Mage :: helper('news') - > ; __('Image'),
'required'=> false,
'name'=>'image',
)); (Mage :: getSingleton('adminhtml / session') - > getNewsData())
{
$ form-> setValues(Mage :: getSingleton(' adminhtml /会话') - > getNewsData());
Mage :: getSingleton('adminhtml / session') - > setNewsData(null);
} elseif(Mage :: registry('news_data')){
$ form-> setValues(Mage :: registry('news_data') - > getData());
}
返回parent :: _ prepareForm();
$ b $和控制器代码
$ b $
$ b <?php
/ **
*
*
* /
class Moo_News_Adminhtml_NewsController extends Mage_Adminhtml_Controller_Action
{
protected function _initAction()
{
$ this-> loadLayout()
- > _setActiveMenu('news / items')
- > _addBreadcrumb(法师:: helper('adminhtml') - &__('Items Manager'),Mage :: helper('adminhtml') - > __('Item Manager'));
返回$ this;
public function indexAction(){
$ this-> _initAction();
$ this-> _addContent($ this-> getLayout() - > createBlock('news / adminhtml_news'));
$ this-> renderLayout();
$ b $ public function editAction()
{
$ newsId = $ this-> getRequest() - > getParam('id');
$ newsModel = Mage :: getModel('news / post') - > load($ newsId);
$ b $ if($ newsModel-> getId()|| $ newsId == 0){
Mage :: register('news_data',$ newsModel);
$ this-> loadLayout();
$ this-> _setActiveMenu('news / items');
$ this-> _addBreadcrumb(Mage :: helper('adminhtml') - > __('Item Manager'),Mage :: helper('adminhtml') - > __('项目经理'));
$ this-> _addBreadcrumb(Mage :: helper('adminhtml') - &_; __('Item News'),Mage :: helper('adminhtml') - &__('Item News') );
$ this-> getLayout() - > getBlock('head') - > setCanLoadExtJs(true);
$ b $ this-> _addContent($ this-> getLayout() - > createBlock('news / adminhtml_news_edit'))
- > _addLeft($ this-> getLayout () - > createBlock( '新闻/ adminhtml_news_edit_tabs'));
$ this-> renderLayout();
} else {
Mage :: getSingleton('adminhtml / session') - > addError(Mage :: helper('news') - > __('Item does not exist'));
$ this-> _redirect('* / * /');
$ b public function newAction()
{
$ this-> _forward('edit');
$ b $ public function saveAction()
{
if($ this-> getRequest() - > getPost()){
try {
$ postData = $ this-> getRequest() - > getPost();
$ newsModel = Mage :: getModel('news / post');
$ imgFilename = NULL;
$ b $ if(isset($ postData ['image'])&& $ postData ['image']!=''){
// if(isset($ _FILES ['image'] ['name'])&& $ _FILES ['image'] ['name']!=''){
if(false){
try {
$ uploader = new Varien_File_Uploader('image');
$ uploader-> setAllowedExtensions(array('jpg','jpeg','gif','png'));
$ uploader-> setAllowRenameFiles(false);
$ uploader-> setFilesDispersion(false);
//将媒体设置为上传目录
$ media_path = Mage :: getBaseDir('media');
$ imgFilename = $ media_path。 $ POSTDATA [图像];
while(file_exists($ filename)){
$ pieces = array();
$ res = preg_match('/ ^(。+)_(\ d +)$ /',$ filename,$ pieces);
if(!$ res){
$ filename。='_1';
} else {
$ filename。='_'。 strval(intval($ pieces [2])+ 1);
$ b $ //上传图片
$ uploader-> save($ media_path,$ postData ['image']);
}
catch(Exception $ e){
Mage :: log($ e);
$ this-> redirectError(502);
}
} else {
if(isset($ postData ['image'] ['delete'])&& $ postData ['image'] ['delete'] = = 1){
$ imgFilename = NULL;
$ b $ if(isset($ imgFilename))
$ newsModel-> setImage($ imgFilename);
$ newsModel-> setId($ this-> getRequest() - > getParam('id'))
- > setTitle($ postData ['title'])
- > setBody($ postData ['body'])
- > setShortDescription($ postData ['short_description'])
- > save();
Mage :: getSingleton('adminhtml / session') - > addSuccess(Mage :: helper('adminhtml') - > __('Item was successfully saved'));
Mage :: getSingleton('adminhtml / session') - > setNewsData(false);
$ this-> _redirect('* / * /');
return;
} catch(Exception $ e){
Mage :: getSingleton('adminhtml / session') - > addError($ e-> getMessage());
Mage :: getSingleton('adminhtml / session') - > setNewsData($ this-> getRequest() - > getPost());
$ this-> _redirect('* / * / edit',array('id'=> $ this-> getRequest() - > getParam('id')));
return;
}
}
$ this-> _redirect('* / * /');
$ b $ public function deleteAction()
{
if($ this-> getRequest() - > getParam('id')> 0) {
try {
$ newsModel = Mage :: getModel('news / post');
$ b $ newsModel-> setId($ this-> getRequest() - > getParam('id'))
- > delete();
Mage :: getSingleton('adminhtml / session') - > addSuccess(Mage :: helper('adminhtml') - > __('Item was successfully deleted'));
$ this-> _redirect('* / * /');
} catch(Exception $ e){
Mage :: getSingleton('adminhtml / session') - > addError($ e-> getMessage());
$ this-> _redirect('* / * / edit',array('id'=> $ this-> getRequest() - > getParam('id')));
}
}
$ this-> _redirect('* / * /');
}
/ **
* AJAX请求的产品网格。
*例如对结果进行排序和过滤。
* /
public function gridAction()
{
$ this-> loadLayout();
$ this-> getResponse() - > setBody(
$ this-> getLayout() - > createBlock('importedit / adminhtml_news_grid') - > toHtml()
);
$ b所以,问题是:我应该怎么做在一个正确的方式吗?
解决方案诀窍是改变默认的表单创建代码 $ form = new Varien_Data_Form(array(
'id'=>'edit_form',
'action'=> $ this-> ; getUrl('* / * / save',array('id'=> $ this-> getRequest() - > getParam('id'))),
'method'=>'后',
));
到
$ form = new Varien_Data_Form(array(
'id'=>'edit_form',
'action'=> $ this-> getUrl('* / * / save' ('id'=> $ this-> getRequest() - > getParam('id'))),
'method'=>'post',
'enctype' =>'multipart / form-data'
));
不在我提供的代码中,但是在调用者内部( ..._ Adminhtml_News_Edit_Form
而不是 ..._ Adminhtml_News_Edit_Tab_Form
)。
I followed this link to make image upload work within my Magento module, but the greatest thing all that code does is passing selected file name via POST: $_FILES
array is empty whenever i add image
or file
fields.
Here's my code:
<?php
class Moo_News_Block_Adminhtml_News_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{
$form = new Varien_Data_Form();
$this->setForm($form);
$fieldset = $form->addFieldset('news_form', array('legend' => Mage::helper('news')->__('Item information')));
$fieldset->addField('title', 'text', array(
'label' => Mage::helper('news')->__('Title'),
'class' => 'required-entry',
'required' => true,
'name' => 'title',
));
$fieldset->addField('published', 'select', array(
'label' => Mage::helper('news')->__('Published'),
'name' => 'published',
'required' => true,
'values' => array(
array(
'value' => 1,
'label' => Mage::helper('news')->__('Yes'),
),
array(
'value' => 0,
'label' => Mage::helper('news')->__('No'),
),
),
));
$fieldset->addField('short_description', 'editor', array(
'name' => 'short_description',
'label' => Mage::helper('news')->__('Short Description'),
'title' => Mage::helper('news')->__('Short Description'),
'style' => 'width:98%; height:200px;',
'wysiwyg' => false,
'required' => true,
));
$fieldset->addField('body', 'editor', array(
'name' => 'body',
'label' => Mage::helper('news')->__('Body'),
'title' => Mage::helper('news')->__('Body'),
'style' => 'width:98%; height:400px;',
'wysiwyg' => false,
'required' => true,
));
$fieldset->addField('image', 'file', array(
'label' => Mage::helper('news')->__('Image'),
'required' => false,
'name' => 'image',
));
if ( Mage::getSingleton('adminhtml/session')->getNewsData() )
{
$form->setValues(Mage::getSingleton('adminhtml/session')->getNewsData());
Mage::getSingleton('adminhtml/session')->setNewsData(null);
} elseif ( Mage::registry('news_data') ) {
$form->setValues(Mage::registry('news_data')->getData());
}
return parent::_prepareForm();
}
}
and the controller code:
<?php
/**
*
*
*/
class Moo_News_Adminhtml_NewsController extends Mage_Adminhtml_Controller_Action
{
protected function _initAction()
{
$this->loadLayout()
->_setActiveMenu('news/items')
->_addBreadcrumb(Mage::helper('adminhtml')->__('Items Manager'), Mage::helper('adminhtml')->__('Item Manager'));
return $this;
}
public function indexAction() {
$this->_initAction();
$this->_addContent($this->getLayout()->createBlock('news/adminhtml_news'));
$this->renderLayout();
}
public function editAction()
{
$newsId = $this->getRequest()->getParam('id');
$newsModel = Mage::getModel('news/post')->load($newsId);
if ($newsModel->getId() || $newsId == 0) {
Mage::register('news_data', $newsModel);
$this->loadLayout();
$this->_setActiveMenu('news/items');
$this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item Manager'), Mage::helper('adminhtml')->__('Item Manager'));
$this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item News'), Mage::helper('adminhtml')->__('Item News'));
$this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
$this->_addContent($this->getLayout()->createBlock('news/adminhtml_news_edit'))
->_addLeft($this->getLayout()->createBlock('news/adminhtml_news_edit_tabs'));
$this->renderLayout();
} else {
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('news')->__('Item does not exist'));
$this->_redirect('*/*/');
}
}
public function newAction()
{
$this->_forward('edit');
}
public function saveAction()
{
if ( $this->getRequest()->getPost() ) {
try {
$postData = $this->getRequest()->getPost();
$newsModel = Mage::getModel('news/post');
$imgFilename = NULL;
//if (isset($postData['image']) && $postData['image'] != '') {
//if (isset($_FILES['image']['name']) && $_FILES['image']['name'] != '') {
if (false) {
try {
$uploader = new Varien_File_Uploader('image');
$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
$uploader->setAllowRenameFiles(false);
$uploader->setFilesDispersion(false);
// Set media as the upload dir
$media_path = Mage::getBaseDir('media');
$imgFilename = $media_path . $postData['image'];
while (file_exists($filename)) {
$pieces = array();
$res = preg_match('/^(.+)_(\d+)$/', $filename, $pieces);
if (!$res) {
$filename .= '_1';
} else {
$filename .= '_' . strval(intval($pieces[2]) + 1);
}
}
// Upload the image
$uploader->save($media_path, $postData['image']);
}
catch (Exception $e) {
Mage::log($e);
$this->redirectError(502);
}
} else {
if(isset($postData['image']['delete']) && $postData['image']['delete'] == 1) {
$imgFilename = NULL;
}
}
if(isset($imgFilename))
$newsModel->setImage($imgFilename);
$newsModel->setId($this->getRequest()->getParam('id'))
->setTitle($postData['title'])
->setBody($postData['body'])
->setShortDescription($postData['short_description'])
->save();
Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully saved'));
Mage::getSingleton('adminhtml/session')->setNewsData(false);
$this->_redirect('*/*/');
return;
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
Mage::getSingleton('adminhtml/session')->setNewsData($this->getRequest()->getPost());
$this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
return;
}
}
$this->_redirect('*/*/');
}
public function deleteAction()
{
if( $this->getRequest()->getParam('id') > 0 ) {
try {
$newsModel = Mage::getModel('news/post');
$newsModel->setId($this->getRequest()->getParam('id'))
->delete();
Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully deleted'));
$this->_redirect('*/*/');
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
$this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
}
}
$this->_redirect('*/*/');
}
/**
* Product grid for AJAX request.
* Sort and filter result for example.
*/
public function gridAction()
{
$this->loadLayout();
$this->getResponse()->setBody(
$this->getLayout()->createBlock('importedit/adminhtml_news_grid')->toHtml()
);
}
}
So, the question is: how should i do this in a right way?
解决方案 The trick was to change the default form creation code
$form = new Varien_Data_Form(array(
'id' => 'edit_form',
'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
'method' => 'post',
));
to
$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'
));
Not within the code i've provided, but within the caller for that partial (..._Adminhtml_News_Edit_Form
instead of ..._Adminhtml_News_Edit_Tab_Form
).
这篇关于Magento图片上传表单字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!