Magento – Can’t Upload CSV File in Custom Module

magento-1.9varien-file-uploader

here is all my file

Form.php

<?php

class Sample_Bigcom_Block_Adminhtml_Form_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
    /**
     * Preparing form
     *
     * @return Mage_Adminhtml_Block_Widget_Form
     */
    protected function _prepareForm()
    {
        $form = new Varien_Data_Form(
            array(
                'id'     => 'edit_form',
                'action' => $this->getUrl('*/*/save'),
                'method' => 'post',
            )
        );

        $form->setUseContainer(true);
        $this->setForm($form);
        $fieldset = $form->addFieldset('bigcom_form', array('legend'=>Mage::helper('sample_bigcom')->__('Project information')));
 $fieldset->addField('file', 'file', array(
          'label'     => Mage::helper('sample_bigcom')->__('Upload'),
          'name'  => 'csvupload',
          'value'  => 'Uplaod',
        //  'after_element_html' => '<small>Comments</small>',
          'tabindex' => 1
        ));

        $fieldset->addField('submit', 'submit', array(
          'label'     => Mage::helper('sample_bigcom')->__('Submit'),
          //'required'  => true,
          'value'  => 'Submit',
          //'after_element_html' => '<small>Comments</small>',
          'tabindex' => 2
        ));
       return parent::_prepareForm();
    }
}

here is my controller

BigcomController.php

<?php

class Sample_Bigcom_Adminhtml_BigcomController extends Mage_Adminhtml_Controller_Action
{


    /**
     * View form action
     */
   public function indexAction()
    {
        $this->loadLayout()
            ->_setActiveMenu('mycustomtab')
            ->_title($this->__('Index Action'));
$block11=$this->getLayout()->createBlock('sample_bigcom/adminhtml_form_edit_form');
$this->getResponse()->setBody($block11->toHtml());
$block=$this->getLayout()->createBlock('sample_bigcom/adminhtml_form_edit');
$this->getResponse()->setBody($block->toHtml());
        // my stuff

        $this->renderLayout();
    }
    public function saveAction()
    {
        print_r($_REQUEST);
        Mage::log($_FILES);
        if ($data = $this->getRequest()->getPost()) {

            $type = 'csvupload';
            echo $_FILES[$type]['name'];
            if(isset($_FILES[$type]['name']) && $_FILES[$type]['name'] != '') {
                try {
                    $uploader = new Varien_File_Uploader('csvupload');
                    $uploader->setAllowedExtensions(array('csv'));
                    $uploader->setAllowRenameFiles(true);
                    $uploader->setFilesDispersion(true);
                    $path = Mage::getBaseDir('media') . DS . 'uploads' . DS;
                    $uploader->save($path, $_FILES[$type]['name'] );
                    $filename = $uploader->getUploadedFileName();

                } catch (Exception $e) {
                }

            }
            echo $filename;
        }

    }


}

i tried lot but every time i got error:

2015-08-08T07:07:08+00:00 DEBUG (7): Array
(
)

2015-08-08T07:07:08+00:00 ERR (3): Notice: Undefined index: csvupload  in C:\xampp\htdocs\magentosample77\app\code\local\Sample\Bigcom\controllers\Adminhtml\BigcomController.php on line 30
2015-08-08T07:07:08+00:00 ERR (3): Notice: Undefined variable: filename  in C:\xampp\htdocs\magentosample77\app\code\local\Sample\Bigcom\controllers\Adminhtml\BigcomController.php on line 45

Best Answer

You also need to change this:

$form = new Varien_Data_Form(array(
            'id'      => 'edit_form',
            'action'  => $this->getUrl('*/*/import'),
            'method'  => 'post',
            'enctype' => 'multipart/form-data'
        ));
Related Topic