[英]Magento admin File Upload not saving the file and path
這只是我對magento管理網格的研究的延續。 我正在嘗試創建文件上傳。 保存過程完成, 沒有錯誤,並且在數據庫中添加了表行。
文件路徑未保存在數據庫中。
沒有創建目錄路徑,當然,沒有文件上傳到服務器。
注意 :該過程已完成,網格和數據庫中還有其他表行。
我只是從此鏈接中引用Magento圖片上傳表單字段
如何為上傳的文件創建目錄文件夾?
如何在數據庫表上保存路徑?
是否在config.xml中添加了代碼?
最后,我解決了文件上傳問題。 感謝@PHP Weblineindia指導我。 這是我更新的控制器。
public function saveAction() {
$post_data=$this->getRequest()->getPost();
if ($post_data) {
try {
//save file to the destination folder
if (isset($_FILES)){
if ($_FILES['file_path']['name']) {
$path = Mage::getBaseDir('media') . DS . 'rts' . DS .'pmadmin'.DS;
$uploader = new Varien_File_Uploader('file_path');
$uploader->setAllowedExtensions(array('PDF','pdf'));
$uploader->setAllowRenameFiles(false);
$uploader->setFilesDispersion(false);
$destFile = $path.$_FILES['file_path']['name'];
$filename = $uploader->getNewFileName($destFile);
$uploader->save($path, $filename);
$post_data['file_path']='rts/pmadmin/'.$filename;
}
}
//save file path to the database
$model = Mage::getModel("pmadmin/pmadmin")
->addData($post_data)
->setId($this->getRequest()->getParam("id"))
->save();
Mage::getSingleton("adminhtml/session")->addSuccess(Mage::helper("adminhtml")->__("File was successfully saved"));
Mage::getSingleton("adminhtml/session")->setPmadminData(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")->setPmadminData($this->getRequest()->getPost());
$this->_redirect("*/*/edit", array("id" => $this->getRequest()->getParam("id")));
return;
}
}
$this->_redirect("*/*/");
}
這是我的表格。
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();
}
路徑應保存在表的file_path列上,文件應上載到media / pmadmin目錄中。
謝謝!
@Rodge,您正在嘗試使用如下所示的發布數據獲取文件數據
$postData = $this->getRequest()->getPost();
$postData['pmadmin_file'];
而且您要上傳文件類型,因此文件類型不屬於發布內容。
請嘗試以下方法獲取文件數據。
$file_name=$_FILES['pmadmin_file']['name'];
並測試是否在saveAction()方法中獲得了文件名,之后便可以存儲文件數據了。 在目錄中。
在您的控制器文件中,您已經保存了以下數據
if(isset($ imgFilename))
$newsModel->setImage($imgFilename);
$newsModel->setId($this->getRequest()->getParam('id'))
->setTitle($postData['title'])
->setShort_description($postData['short_description'])
->setCustomer_group_id($postData['customer_group_id'])
->setIs_active($postData['is_active'])
->setValue($postData['value'])
->save();
嘗試像這樣保存數據
if(isset($ imgFilename)){
$newsModel->setId($this->getRequest()->getParam('id'))
->setTitle($postData['title'])
->setFilePath($imgFilename)
->setShortDescription($postData['short_description'])
->setCustomerGroupId($postData['customer_group_id'])
->setIsActive($postData['is_active'])
->setValue($postData['value'])
->save();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.