magento 自定义模块使用uploader上传文件
1、在edit/form.php中添加代码:addField('images', 'note', array( 'label' => Mage::helper('review')->__('Review Image'), //'required' => true, 'name' => 'images', 'text' => $this->getLayout()->createBlock('xreview/adminhtml_review_image_upload')->_toHtml(), ));?>2、新建block:silk/xreview/adminhtml/review/image/upload.phpsetTemplate('review/image/upload.phtml'); } protected function _prepareLayout() { //加载Uploader插件的js $this->getLayout()->getBlock('head')->addJs('lib/flex.js'); $this->getLayout()->getBlock('head')->addJs('mage/adminhtml/flexuploader.js'); $this->getLayout()->getBlock('head')->addJs('lib/FABridge.js'); // 添加Mage_Adminhtml_Block_Media_Uploader子块Block $this->setChild('uploader', $this->getLayout()->createBlock('adminhtml/media_uploader')); $this->getChild('uploader')->getConfig() // 文件上传处理action ->setUrl(Mage::getModel('adminhtml/url')->addSessionParam()->getUrl('xreview/adminhtml_xreview/upload')) ->setFileField('review_image') ->setFilters(array( 'images' => array( 'label' => Mage::helper('adminhtml')->__('Images (.gif, .jpg, .png)'), 'files' => array('*.gif', '*.jpg','*.jpeg', '*.png') ) )); return parent::_prepareLayout(); }}?>3、上一步,声明了blcok对应的phtml,所以新建design/adminhtml/default/default/template/review/image/upload.phtml: getChildHtml('uploader')?>以上步骤完成以后,就可以看见与产品edit页面image中的2个按钮了4、新建controller:silk/xreview/controllers/adminhtml/xreviewController.phpsetAllowedExtensions(array('jpg','jpeg','gif','png')); $uploader->setAllowRenameFiles(true); $uploader->setFilesDispersion(true); $result = $uploader->save($this->getBaseTmpMediaPath()); Mage::dispatchEvent('catalog_product_gallery_upload_image_after', array( 'result' => $result, 'action' => $this )); $result['tmp_name'] = str_replace(DS, "/", $result['tmp_name']); $result['path'] = str_replace(DS, "/", $result['path']); $result['url'] =$this->getTmpMediaUrl($result['file']); $result['file'] = $result['file']; $result['cookie'] = array( 'name' => session_name(), 'value' => $this->_getSession()->getSessionId(), 'lifetime' => $this->_getSession()->getCookieLifetime(), 'path' => $this->_getSession()->getCookiePath(), 'domain' => $this->_getSession()->getCookieDomain() ); } catch (Exception $e) { $result = array( 'error' => $e->getMessage(), 'errorcode' => $e->getCode()); } //Mage::log($result,null,'cookie.log'); $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result)); } private function getBaseTmpMediaPath() { return Mage::getBaseDir('media') . DS . 'review'; } private function getTmpMediaUrl($file) { $file = str_replace(DS, '/', $file); if (substr($file, 0, 1) == '/') { $file = substr($file, 1); } return Mage::getBaseUrl('media') . 'review/'. $file; }}?>5、如何处理action的json数据,在upload.phtml中,加入下面的js: //处理action中的json数据 var uploader = getChild('uploader')->getJsObjectName(); ?>; uploader.onFilesComplete = function(files) { var i = 0; files.each(function(file) { if(document.getElementById('upload_image_'+i)){ document.getElementById('upload_image_'+i).remove(); } var str = file.response; var json = eval('('+str+')'); //图片以隐藏域的方式提交 var input = document.createElement('input'); input.setAttribute('type', 'hidden'); input.setAttribute('value', json.file); input.setAttribute('id', 'upload_image_'+i); input.setAttribute('name', 'images[]'); document.getElementById('add_review_form').appendChild(input); i++; }); }
关键字:magento
版权声明
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处。如若内容有涉嫌抄袭侵权/违法违规/事实不符,请点击 举报 进行投诉反馈!