简体   繁体   English

从自定义管理模块在Magento中无法进行文件上传

[英]File Upload not working in Magento from custom admin module

I created a custom admin module in magento. 我在magento中创建了一个自定义管理模块。 The module has a form which on submitting posts to a controller which handles the file upload. 该模块具有一个表单,该表单将帖子提交给处理文件上传的控制器。

I am getting the following error on submitting the form: 提交表单时出现以下错误:

a:5:{i:0;s:22:"File was not uploaded.";i:1;s:1030:"#0 C:\wamp\www\magento-PhpDevGuide-packt\app\code\local\Endosoft\Categoryupdate\controllers\Adminhtml\CategoryupdateController.php(11): Varien_File_Uploader->__construct('uploadcsv')
#1 C:\wamp\www\magento-PhpDevGuide-packt\app\code\core\Mage\Core\Controller\Varien\Action.php(418): Endosoft_Categoryupdate_Adminhtml_CategoryupdateController->formPostAction()
#2 C:\wamp\www\magento-PhpDevGuide-packt\app\code\core\Mage\Core\Controller\Varien\Router\Standard.php(250): Mage_Core_Controller_Varien_Action->dispatch('formpost')
#3 C:\wamp\www\magento-PhpDevGuide-packt\app\code\core\Mage\Core\Controller\Varien\Front.php(172): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#4 C:\wamp\www\magento-PhpDevGuide-packt\app\code\core\Mage\Core\Model\App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#5 C:\wamp\www\magento-PhpDevGuide-packt\app\Mage.php(684): Mage_Core_Model_App->run(Array)
#6 C:\wamp\www\magento-PhpDevGuide-packt\index.php(87): Mage::run('', 'store')
#7 {main}";s:3:"url";s:78:"/index.php/admin/categoryupdate/formpost/key/d91e6825136ce2206d7c72bbd1b7af34/";s:11:"script_name";s:10:"/index.php";s:4:"skin";s:5:"admin";}

Here is the form: 形式如下:

<?php $url = Mage::helper("adminhtml")->getUrl('adminhtml/categoryupdate/formpost'); ?>
<h4>Upload Your CSV</h4>
<form action="<?php echo $url ?>" method="post" enctype="multipart/form-data">
    <p><input name="form_key" type="hidden" value="<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" /></p>
    <p><input type="file" name="csvupload" /></p>
    <p><input type="submit" name="submit" value="Submit" /></p>
</form>

Here is the controller method: 这是控制器方法:

public function formPostAction() {
  $uploader = new Varien_File_Uploader('uploadcsv');
  $uploader->setAllowedExtensions(array('json'));
  $uploader->setAllowRenameFiles(false);
  $uploader->setFilesDispersion(false);
  $path = Mage::getBaseDir('media');
  $fileName = $_FILES['uploadcsv']['name'];
  $uploader->save($path, $fileName);

  $this->loadLayout();
  $this->renderLayout();
}

I could not understand why the file is not uploaded. 我不明白为什么文件没有上传。

Could someone please clarify what is going wrong? 有人可以澄清出什么问题了吗?

Additional Info: 附加信息:

  • Instead of using magento form container and form i am using my own form by calling a phtml file from the layout.. 我不是使用magento表单容器和表单,而是通过从布局中调用phtml文件来使用自己的表单。

I see that your file input looks like this: 我看到您的文件输入如下所示:

<input type="file" name="csvupload" />

Notice the name csvupload . 注意名称csvupload
But in the formpostAction you are trying to read the uploadcsv field. 但是在formpostAction您正在尝试读取uploadcsv字段。
Make sure that what you submit matches what you process. 确保您提交的内容与您处理的内容匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM