上周我为我的 Magento 应用程序添加了文件上传选项。这是代码。这可能对您有所帮助。这是您的控制器的保存操作。
public function saveAction() {
$filedata = array();
$uploadedFile = 'uploaded_file';
$_helper = Mage::helper('fileuploader');
if (!empty($_FILES[$uploadedFile]['name'])) {
try {
$ext = $_helper->getFileExtension($_FILES[$uploadedFile]['name']);
$fname = 'File-' . time() . $ext;
$uploader = new Varien_File_Uploader($uploadedFile);
#$uploader->setAllowedExtensions(array("txt", "csv", "htm", "html", "xml", "css", "doc", "docx", "xls", "xlsx", "rtf", "ppt", "pdf", "swf", "flv", "avi", "wmv", "mov", "wav", "mp3", "jpg", "jpeg", "gif", "png","zip"));
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(false);
$path = Mage::getBaseDir('media') . DS . 'custom' . DS . 'upload' . DS;
$uploader->save($path, $fname);
$filedata[$uploadedFile] = 'custom/upload/' . $fname;
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
$this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
return;
}
}
if ($data = $this->getRequest()->getPost()) {
$products = array();
$availProductIds = Mage::getModel('fileuploader/fileuploader')->getAllAvailProductIds();
parse_str($data['fileuploader_products'], $products);
foreach ($products as $k => $v) {
if (preg_match('/[^0-9]+/', $k) || preg_match('/[^0-9]+/', $v)) {
unset($products[$k]);
}
}
$productIds = array_intersect($availProductIds, $products);
$data['product_ids'] = implode(',', $productIds);
if (!empty($filedata[$uploadedFile])) {
$data[$uploadedFile] = $filedata[$uploadedFile];
} else {
if (isset($data[$uploadedFile]['delete']) && $data[$uploadedFile]['delete'] == 1) {
if ($data[$uploadedFile]['value'] != '')
$this->removeFile($data[$uploadedFile]['value']);
$data[$uploadedFile] = '';
}else {
unset($data[$uploadedFile]);
}
}
$model = Mage::getModel('fileuploader/fileuploader');
$model->setData($data)
->setId($this->getRequest()->getParam('id'));
try {
$model->save();
Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('fileuploader')->__('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('fileuploader')->__('Unable to find item to save'));
$this->_redirect('*/*/');
}
通过在 grid.php 中添加 colomn 函数来构建表单
$this->addColumn('uploaded_file', array(
'header' => Mage::helper('fileuploader')->__('File'),
'align' => 'left',
'type' => 'file',
'escape' => true,
'sortable' => false,
'index' => 'uploaded_file',
));
这是最重要的。不要忘记提及您的表单 enctype 应该是 form.php 文件中的 Multi/part。我猜你错过了。请检查这个。
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'
)
);
而已..!!